Parsing HTML from scraped pages
As we already saw, we can easily download a webpage in Python with urllib
or the requests
library. One thing to note is these libraries do not work with dynamic JavaScript content. For example, we can download and save the Packt home page:
res = rq.get('https://www.packtpub.com/')
with open('packt.html', 'wb') as f:
f.write(res.content)
When we open it, it looks OK, except not all the content displays properly. For example, some of the icons are missing because they load dynamically with JavaScript. For gathering data from pages where JavaScript plays a heavy role, we can instead use other packages, like requests-html
, Selenium, or the Scrapy package with the scrapy-splash
plugin. The point is, you may notice some data missing when scraping data from pages. This could be due to JavaScript loading content, meaning you should try one of those other packages for scraping data from that page.
As our first example...