MIT*_*THU 8 python json web-scraping python-3.x python-requests
我已经在python中创建了一个脚本来解析网页中的两个字段- total revenue这很令人担忧date。我关注的字段是javascript加密的。它们在json数组中的页面源中可用。以下脚本可以相应地解析这两个字段。
但是,问题在于该页面中的可见日期与页面源中可用的日期不同。
该网页中的日期是这样的
页面源中的日期是这样的
显然会有一天的变化。
当您单击该选项卡访问该网页后,Quarterly您可以在其中看到结果:
我尝试过:
import re
import json
import requests
url = 'https://finance.yahoo.com/quote/GTX/financials?p=GTX'
res = requests.get(url)
data = re.findall(r'root.App.main[^{]+(.*);',res.text)[0]
jsoncontent = json.loads(data)
container = jsoncontent['context']['dispatcher']['stores']['QuoteSummaryStore']['incomeStatementHistoryQuarterly']['incomeStatementHistory']
total_revenue = container[0]['totalRevenue']['raw']
concerning_date = container[0]['endDate']['fmt']
print(total_revenue,concerning_date)
Run Code Online (Sandbox Code Playgroud)
我得到的结果(以百万为单位的收入):
802000000 2019-06-30
Run Code Online (Sandbox Code Playgroud)
结果我希望得到:
802000000 2019-06-29
Run Code Online (Sandbox Code Playgroud)
当我尝试使用此行情自动AAPL收录器时,我会得到确切的日期,因此不能选择跟踪或添加日期。
如何从该站点获取确切日期?
顺便说一句,我知道如何使用硒来获得它们,所以我只想坚持requests。
小智 1
与其获取fmt的concerning_date,不如获取时间戳。
concerning_date = container[0]['endDate']['raw']
Run Code Online (Sandbox Code Playgroud)
在上面的示例中,您将得到结果1561852800,您可以将其转换为具有特定时区的日期。(提示:使用datetime和pytz)。该时间戳将根据时区产生以下结果:
Date in Los Angeles*: 29/06/2019, 17:00:00
Date in Berlin* :30/06/2019, 02:00:00
Date in Beijing*: 30/06/2019, 07:00:00
Date in New York* :29/06/2019, 19:00:00
Run Code Online (Sandbox Code Playgroud)