diff --git a/web-scraping/example.html b/web-scraping/example.html new file mode 100644 index 0000000..cf48143 --- /dev/null +++ b/web-scraping/example.html @@ -0,0 +1,7 @@ +The Website Title + +

Download my Python book from my website.

+

Learn Python the easy way!

+

By Al Sweigart

+ diff --git a/web-scraping/google-search.py b/web-scraping/google-search.py new file mode 100644 index 0000000..8453f11 --- /dev/null +++ b/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') diff --git a/web-scraping/parse-html.py b/web-scraping/parse-html.py new file mode 100644 index 0000000..ae79807 --- /dev/null +++ b/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/ +