Browse Source

parsing

master
Gharke 4 months ago
parent
commit
dff81a3258
  1. 7
      web-scraping/example.html
  2. 19
      web-scraping/google-search.py
  3. 15
      web-scraping/parse-html.py

7
web-scraping/example.html

@ -0,0 +1,7 @@
<html><head><title>The Website Title</title></head>
<body>
<p>Download my <strong>Python</strong> book from <a href="http://
inventwithpython.com">my website</a>.</p>
<p class="slogan">Learn Python the easy way!</p>
<p>By <span id="author">Al Sweigart</span></p>
</body></html>

19
web-scraping/google-search.py

@ -0,0 +1,19 @@
#! python3
# lucky.py - open several Google search results
import requests, sys, webbrowser, bs4
print('Googling...') # display text while downloading the Google page
res = requests.get('http://google.com/search?q=' +''.join(sys.argv[1:]))
res.raise_for_status()
# retrieve the top search result links
soup = bs4.BeautifulSoup(res.text, 'html.parser')
# open a browser tab for each
linkElems = soup.select('.LC201b h3')
numOpen = min(5, len(linkElems))
print('Opening {} links.'.format(numOpen))
for i in range(numOpen):
webbrowser.open('http://google.com' + linkElems[i].get('href'))
linkElems[i].get('href')

15
web-scraping/parse-html.py

@ -0,0 +1,15 @@
import requests, bs4
res = requests.get('https://www.nostarch.com')
res.raise_for_status()
noStarchSoup = bs4.BeautifulSoup(res.text)
print(type(noStarchSoup))
print(noStarchSoup.select('#main > p:nth-of-type(2)'))
print(noStarchSoup.select('p')[1])
print(noStarchSoup.select('p > a')[0].getText())
# https://beautiful-soup-4.readthedocs.io/en/latest/
Loading…
Cancel
Save