guys! I am applying to you once again. I am ok with scraping simple websites with tags but recently I've encountered a quite plex website which has JavaScript. As a result I would like to obtain all the estimates at the bottom of the page in a format of table (csv). Like 'User', 'Revenue estimate', 'EPS estimate'.
I hoped to figure it by myself but kinda failed.
Here is my code:
from urllib import urlopen
from bs4 import BeautifulSoup
html = urlopen(";direction=asc&estimates_per_page=142&show_confirm=false")
soup = BeautifulSoup(html.read(), "html.parser")
print(soup.findAll('script')[11].string.encode('utf8'))
The output has a strange format and I don't know how to extract the data in an adequate form. I'll appreciate any help!
guys! I am applying to you once again. I am ok with scraping simple websites with tags but recently I've encountered a quite plex website which has JavaScript. As a result I would like to obtain all the estimates at the bottom of the page in a format of table (csv). Like 'User', 'Revenue estimate', 'EPS estimate'.
I hoped to figure it by myself but kinda failed.
Here is my code:
from urllib import urlopen
from bs4 import BeautifulSoup
html = urlopen("https://www.estimize./jpm/fq3-2016?sort=rank&direction=asc&estimates_per_page=142&show_confirm=false")
soup = BeautifulSoup(html.read(), "html.parser")
print(soup.findAll('script')[11].string.encode('utf8'))
The output has a strange format and I don't know how to extract the data in an adequate form. I'll appreciate any help!
Share Improve this question asked Mar 30, 2017 at 14:13 Anna IgnashkinaAnna Ignashkina 4975 silver badges17 bronze badges 1- 1 Use selenium to scrap webs with javascript – Wonka Commented Mar 30, 2017 at 14:15
2 Answers
Reset to default 3Looks like the data you're trying to extract is in a data model, which means it's in JSON. If you do a small amount of parsing with the following:
import json
import re
data_string = soup.findAll('script')[11].string.encode('utf8')
data_string = data_string.split("DataModel.parse(")[1]
data_string = data_string.split(");")[0]
// parse out erroneous html
while re.search('\<[^\>]*\>', datastring):
data_string = ''.join(datastring.split(re.search('\<[^\>]*\>', datastring).group(0)))
// parse out other function parameters, leaving you with the json
data_you_want = json.loads(data_string.split(re.search('\}[^",\}\]]+,', data_string).group(0))[0]+'}')
print(data_you_want["estimate"])
>>> {'shares': {'shares_hash': {'twitter': None, 'stocktwits': None, 'linkedin': None}}, 'lastRevised': None, 'id': None, 'revenue_points': None, 'sector': 'financials', 'persisted': False, 'points': None, 'instrumentSlug': 'jpm', 'wallstreetRevenue': 23972, 'revenue': 23972, 'createdAt': None, 'username': None, 'isBlind': False, 'releaseSlug': 'fq3-2016', 'statement': '', 'errorRanges': {'revenue': {'low': 21247.3532016398, 'high': 26820.423240734}, 'eps': {'low': 1.02460526459765, 'high': 1.81359679579922}}, 'eps_points': None, 'rank': None, 'instrumentId': 981, 'eps': 1.4, 'season': '2016-fall', 'releaseId': 52773}
The DataModel.parse is a javascript method which means it ends with a parenthesis and a colon. the parameter for the function is the JSON object you want. By loading it into json.loads you're able to access it much like a dictionary.
From there you remap the data into the form you want it to be in for the csv.
This is how I've resolved the problem, using some tips above:
from bs4 import BeautifulSoup
from urllib import urlopen
import json
import csv
f = csv.writer(open("estimize.csv", "a"))
f.writerow(["User Name", "Revenue Estimate", "EPS Estimate"])
html = "https://www.estimize./jpm/fq3-2016?sort=rank&direction=asc&estimates_per_page=142&show_confirm=false"
html = urlopen(html)
soup = BeautifulSoup(html.read(), "html.parser").encode('utf8')
data_string = soup.split("\"allEstimateRows\":")[1]
data_string = data_string.split(",\"tableSortDirection")[0]
data = json.loads(data_string)
for item in data:
f.writerow([item["userName"], item["revenue"], item["eps"]])