Skip to content

Commit 4b403cf

Browse files
Winding Up
1 parent 56563a5 commit 4b403cf

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import requests, bs4
2+
res = requests.get('http://nostarch.com')
3+
res.raise_for_status()
4+
noStarchSoup = bs4.BeautifulSoup(res.text,"lxml")
5+
print(type(noStarchSoup))
6+
7+
exampleFile = open('example.html')
8+
exampleSoup = bs4.BeautifulSoup(exampleFile,"lxml")
9+
print(type(exampleSoup))
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!-- This is the example.html example file. -->
2+
3+
<html><head><title>The Website Title</title></head>
4+
<body>
5+
<p>Download my <strong>Python</strong> book from <a href="http://
6+
inventwithpython.com">my website</a>.</p>
7+
<p class="slogan">Learn Python the easy way!</p>
8+
<p>By <span id="author">Al Sweigart</span></p>
9+
</body></html>

Chapter11-Web Scraping/lucky.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#! /usr/bin/python3.5
2+
3+
"""
4+
5+
Author: Samrat Banerjee
6+
Dated: 17/08/2018
7+
Description: Project: “I’m Feeling Lucky” Google Search- Opens several Google search results
8+
9+
"""
10+
11+
import requests,sys,webbrowser,bs4
12+
13+
print('Googling...') # display text while downloading the Google page
14+
res=requests.get('https://google.com/search?q=' + ' '.join(sys.argv[1:]))
15+
res.raise_for_status()
16+
17+
# Retrieve top search result links
18+
soup=bs4.BeautifulSoup(res.text,"lxml")
19+
20+
# Open a browser tab for each result
21+
linkElems=soup.select('.r a')
22+
numOpen=min(5,len(linkElems))
23+
for i in range(numOpen):
24+
webbrowser.open('https://google.com' + linkElems[i].get('href'))

Chapter11-Web Scraping/mapIt.py

100644100755
File mode changed.

0 commit comments

Comments
 (0)