Reputation: 327
I am working on a project in which I need to crawl several websites and gather different kinds of information from them. Information like text, links, images, etc.
I am using Python for this. I have tried BeautifulSoup for this purpose on the HTML pages and it works, but I am stuck when parsing sites which contains a lot of JavaScript, as most of the information on these files is stored in the <script>
tag.
Any ideas how to do this?
Upvotes: 5
Views: 13643
Reputation: 8614
First of all, scrapping and parsing JS from pages is not trivial. It can however be vastly simplified if you use a headless web client instead, which will parse everything for you just like a regular browser would.
The only difference is that its main interface is not GUI/HMI but an API.
For example, you can use PhantomJS with Chrome or Firefox which both support headless mode.
For a more complete list of headless browsers check here.
Upvotes: 4
Reputation: 17511
A very fast way would be to iterate through all the tags and get textContent
This is the JS snippet:
page =""; var all = document.getElementsByTagName("*"); for (tag of all) page = page + tag.textContent;
or in selenium/python:
import selenium
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("http://ranprieur.com")
pagetext = driver.execute_script('page =""; var all = document.getElementsByTagName("*"); for (tag of all) page = page + tag.textContent; return page;')
Upvotes: 0
Reputation: 18375
To get you started with selenium and BeautifulSoup:
Install phantomjs with npm (Node Package Manager):
apt-get install nodejs
npm install phantomjs
install selenium:
pip install selenium
and get the resulted page like this, and parse it with beautifulSoup as usual:
from BeautifulSoup4 import BeautifulSoup as bs
from selenium import webdriver
client = webdriver.PhantomJS()
client.get("http://foo")
soup = bs(client.page_source)
Upvotes: 0
Reputation: 473753
If there is a lot of javascript dynamic load involved in the page loading, things get more complicated.
Basically, you have 3 ways to crawl the data from the website:
Also take a look at Scrapy web-scraping framework - it doesn't handle AJAX calls too, but this is really the best tool in web-scraping world I've ever worked with.
Also see these resources:
Hope that helps.
Upvotes: 2