updates · ankitom/python-scripts@53da94f · GitHub
Skip to content

Commit 53da94f

Browse files
committed
updates
1 parent 239a0ff commit 53da94f

3 files changed

Lines changed: 37 additions & 53 deletions

File tree

08_basic_email_web_crawler.py

Lines changed: 20 additions & 24 deletions

09_basic_link_web_crawler.py

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,27 @@
66
link_re = re.compile(r'href="(.*?)"')
77

88

9-
def crawl(url, maxlevel):
9+
def crawl(url):
1010

11-
result = set()
11+
req = requests.get(url)
1212

13-
while maxlevel > 0:
13+
# Check if successful
14+
if(req.status_code != 200):
15+
return []
1416

15-
# Get the webpage
16-
req = requests.get(url)
17+
# Find links
18+
links = link_re.findall(req.text)
1719

18-
# Check if successful
19-
if(req.status_code != 200):
20-
return []
20+
print "\nFound {} links".format(len(links))
2121

22-
# Find and follow all the links
23-
links = link_re.findall(req.text)
24-
for link in links:
25-
# Get an absolute URL for a link
26-
link = urlparse.urljoin(url, link)
27-
# add links to result set
28-
result.update(link)
22+
# Search links for emails
23+
for link in links:
2924

30-
print "Crawled level: {}".format(maxlevel)
25+
# Get an absolute URL for a link
26+
link = urlparse.urljoin(url, link)
3127

32-
# new level
33-
maxlevel -= 1
28+
print link
3429

35-
# recurse
36-
crawl(link, maxlevel)
3730

38-
return result
39-
40-
emails = crawl('http://www.website_goes_here_dot_com', 2)
41-
42-
print "\nScrapped links:"
43-
for link in links:
44-
print link
31+
if __name__ == '__main__':
32+
crawl('http://www.realpython.com')

readme.md

Lines changed: 2 additions & 2 deletions

0 commit comments

Comments
 (0)