Sponsored Content
Top Forums Programming Python Web Page Scraping Urls Creating A Dictionary Post 302998728 by durden_tyler on Tuesday 6th of June 2017 01:57:11 PM
Old 06-06-2017
Quote:
Originally Posted by metallica1973
...
...
but I want it in a dictionary with this format:
Code:
bigbadwolf.htlm: https://www.blah.com, http://www.blahblah.com, http://www.blahblahblah.com
maryhadalittlelamb.html: http://www.red.com, https://www.redyellow.com, http://www.zigzag.com
time.html: https://www.est.com, http://www.pst.com, https://www.cst.com

As you can see, there will be several urls inside of an html doc so there will be keys that can contain many values(urls).
...
...
Is each value of the dictionary:
(a) a list (or array) of URLs? or
(b) a comma-delimited string of URLs?

If you want (a), then try something like the following:

Code:
tut_links = {}
for subdir, dirs, files in os.walk('./html/tutorials/blah'):
    for tut in files:
        if tut.endswith(".html"):
            tut_links[tut] = []
            fpath = os.path.join("./html/tutorials/blah", tut)
            content = open(fpath, "r").read()
            file = BeautifulSoup(content, 'lxml')
            for links in file.find_all('a'):
                urls = links.get('href')
                tut_links[tut].append(urls)

Disclaimer: Completely untested; I don't have the module at the moment.
These 2 Users Gave Thanks to durden_tyler For This Post:
 

8 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

making a web page

Hey im new to unix! I am tryin to create a web page in unix and have done it all but when i try and load it it says permission denied!?> i have chmod a+rx for folder and file to make sure but still permissions wont let me?! any ideas can anyone do a quick run through of how to make a web page... (4 Replies)
Discussion started by: shashora
4 Replies

2. UNIX for Dummies Questions & Answers

Accessing Web Page

Hello, I am new to unix, but wanted to know how can we fetch data from a web page (i.e. an HTML Page), my requirement is to read an html page and wanted to create a flat file (text file) based on the contents available in the mentioned HTML page. Thanks Imtiaz (3 Replies)
Discussion started by: Imtiaz
3 Replies

3. UNIX for Dummies Questions & Answers

how do i make a web page

hey uhh this is my first post and i was wondering how do i make a web page for like a small business or something anything will help thanks (3 Replies)
Discussion started by: Neil Peart
3 Replies

4. UNIX for Dummies Questions & Answers

Make a Web page

I'm 13 years of age and I am into computers. I am trying to learn how to make a webpage. I could use the help and I would greatly appriciate it. (1 Reply)
Discussion started by: lydia98
1 Replies

5. Programming

fetching a web page in C

Hello, I'm a total newbie to HTTP commands, so I'm not sure how to do this. What I'd like is to write a C program to fetch the contents of a html page of a given address. Could someone help with this? Thanks in advance! (4 Replies)
Discussion started by: rayne
4 Replies

6. Shell Programming and Scripting

Creating a dictionary with domain name adjuncted

Hello, I have created a dictionary which has the following structure: DOMAINWORD=(equivalent in English)gloss(es) in Hindi each separated by a comma(equivalent in English)gloss(es) in Hindi each separated by a comma or a semi-colon An example will make this clear ... (13 Replies)
Discussion started by: gimley
13 Replies

7. Shell Programming and Scripting

Counting all words that start with a capital letter in a string using python dictionary

Hi, I have written the following python snippet to store the capital letter starting words into a dictionary as key and no of its appearances as a value in this dictionary against the key. #!/usr/bin/env python import sys import re hash = {} # initialize an empty dictinonary for line in... (1 Reply)
Discussion started by: royalibrahim
1 Replies

8. Shell Programming and Scripting

Creating verbal structures from a dictionary and a template

My main aim here is to create a database of verbs in a language to Hindi. The output if it works well will be put up on a University site for researchers to use for Machine Translation. This because one of the main weaknesses of MT is in the area of verbs. Sorry for the long post but the problem... (4 Replies)
Discussion started by: gimley
4 Replies
HTML::FormatText::WithLinks(3)				User Contributed Perl Documentation			    HTML::FormatText::WithLinks(3)

NAME
HTML::FormatText::WithLinks - HTML to text conversion with links as footnotes SYNOPSIS
use HTML::FormatText::WithLinks; my $f = HTML::FormatText::WithLinks->new(); my $html = qq( <html> <body> <p> Some html with a <a href="http://example.com/">link</a> </p> </body> </html> ); my $text = $f->parse($html); print $text; # results in something like Some html with a [1]link 1. http://example.com/ my $f2 = HTML::FormatText::WithLinks->new( before_link => '', after_link => ' [%l]', footnote => '' ); $text = $f2->parse($html); print $text; # results in something like Some html with a link [http://example.com/] my $f3 = HTML::FormatText::WithLinks->new( link_num_generator => sub { return "*" x (shift() + 1); }, footnote => '[%n] %l' ); $text = $f3->parse($html); print $text; # results in something like Some html with a [*]link [*] http://example.com/ DESCRIPTION
HTML::FormatText::WithLinks takes HTML and turns it into plain text but prints all the links in the HTML as footnotes. By default, it attempts to mimic the format of the lynx text based web browser's --dump option. METHODS
new my $f = HTML::FormatText::WithLinks->new( %options ); Returns a new instance. It accepts all the options of HTML::FormatText plus base a base option. This should be set to a URI which will be used to turn any relative URIs on the HTML to absolute ones. doc_overrides_base If a base element is found in the document and it has an href attribute then setting doc_overrides_base to true will cause the document's base to be used. This defaults to false. before_link (default: '[%n]') after_link (default: '') footnote (default: '[%n] %l') a string to print before a link (i.e. when the <a> is found), after link has ended (i.e. when then </a> is found) and when printing out footnotes. "%n" will be replaced by the link number, "%l" will be replaced by the link itself. If footnote is set to '', no footnotes will be printed. link_num_generator (default: sub { return shift() + 1 }) link_num_generator is a sub that returns the value to be printed for a given link number. The internal store starts numbering at 0. with_emphasis If set to 1 then italicised text will be surrounded by "/" and bolded text by "_". You can change these markers by using the "italic_marker" and "bold_marker" options. unique_links If set to 1 then will only generate 1 footnote per unique URI as oppose to the default behaviour which is to generate a footnote per URI. anchor_links If set to 0 then links pointing to local anchors will be skipped. The default behaviour is to include all links. skip_linked_urls If set to 1, then links where the text equals the href value will be skipped. The default behaviour is to include all links. parse my $text = $f->parse($html); Takes some HTML and returns it as text. Returns undef on error. Will also return undef if you don't pass it undef. Returns an empty string if passed an empty string. parse_file my $text = $f->parse_file($filename); Takes a filename and returns the contents of the file as plain text. Returns undef on error. error $f->error(); Returns the last error that occured. In practice this is likely to be either a warning that parse_file couldn't find the file or that HTML::TreeBuilder failed. CAVEATS
When passing HTML fragments the results may be a little unpredictable. I've tried to work round the most egregious of the issues but any unexpected results are welcome. Also note that if for some reason there is an a tag in the document that does not have an href attribute then it will be quietly ignored. If this is really a problem for anyone then let me know and I'll see if I can think of a sensible thing to do in this case. AUTHOR
Struan Donald. <struan@cpan.org> <http://www.exo.org.uk/code/> Ian Malpass <ian@indecorous.com> was responsible for the custom formatting bits and the nudge to release the code. Simon Dassow <janus@errornet.de<gt> for the anchor_links option plus a few bugfixes and optimisations Kevin Ryde for the code for pulling the base out the document. Thomas Sibley <trs@bestpractical.com> patches for skipping links that are their urls and to change the delimiters for bold and italic text.. COPYRIGHT
Copyright (C) 2003-2010 Struan Donald and Ian Malpass. All rights reserved. LICENSE
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. SEE ALSO
perl(1), HTML::Formatter. perl v5.16.3 2010-12-09 HTML::FormatText::WithLinks(3)
All times are GMT -4. The time now is 11:18 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy