无法使用请求解析网页的确切结果 [英] Unable to parse an exact result from a webpage using requests

查看:135
本文介绍了无法使用请求解析网页的确切结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在python中创建了一个脚本来解析网页中的两个字段-total revenue,它与date有关.我关注的字段是用javascript加密的.它们在json数组中的页面源中可用.以下脚本可以相应地解析这两个字段.

I've created a script in python to parse two fields from a webpage - total revenue and it's concerning date. The fields I'm after are javascript encrypted. They are available in page source within json array. The following script can parse those two fields accordingly.

但是,问题在于该页面中的可见日期与页面源中的可用日期不同.

However, the problem is the date visible in that page is different from the one available in page source.

网页链接

该网页中的日期类似于

The date in that webpage is like this

页面源中的日期类似于

The date in page source is like this

显然会有一天的变化.

访问该 网页 ,当您点击此标签 Quarterly 时,您可以在其中看到结果:

After visiting that webpage when you click on this tab Quarterly you can see the results there:

我尝试过:

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)

我得到的结果(以百万为单位的收入):

Result I get (revenue in million):

802000000 2019-06-30

我希望得到的结果:

802000000 2019-06-29

当我尝试使用此股票代码AAPL时,我会得到确切的日期,因此不能追踪或添加日期.

When I try with this ticker AAPL, I get the exact date, so subtracing or adding a day is not an option.

如何从该站点获取确切日期 ?

How can I get the exact date from that site?

顺便说一句,我知道如何使用硒来获得它们,所以我只想坚持使用requests.

Btw, I know how to get them using selenium, so I would only like to stick to requests.

推荐答案

此答案的更新部分概述了日期差异的根本原因.

原始答案

JSON中的某些原始值是UNIX时间戳.

Some of the raw values in your JSON are UNIX timestamps.

修改后的代码引用:

concerning_date_fmt = container[0]['endDate']['fmt']
concerning_date_raw = container[0]['endDate']['raw']
print(f'{concerning_date} -- {concerning_date_raw}')
# output 
2019-07-28 -- 1564272000 

'endDate':{'fmt':'2019-07-28','raw':1564272000}

'endDate': {'fmt': '2019-07-28', 'raw': 1564272000}

1564272000 是自1970年1月1日以来经过的秒数.此日期是Unix纪元的开始,时间以世界标准时间(UTC)为单位. 1564272000 等效于:07/28/2019 12:00 am(UTC).

1564272000 is the number of elapsed seconds since January 01 1970. This date was the start of the Unix Epoch and the time is in Coordinated Universal Time (UTC). 1564272000 is the equivalent to: 07/28/2019 12:00am (UTC).

您可以使用内置的Python函数

You can covert these timestamps to a standard datetime format by using built-in Python functions

from datetime import datetime
unix_timestamp = int('1548547200')

converted_timestamp = datetime.utcfromtimestamp(unix_timestamp).strftime('%Y-%m-%dT%H:%M:%SZ')
print (converted_timestamp)
# output Coordinated Universal Time (or UTC)
2019-07-28T00:00:00Z

reformatted_timestamp = datetime.strptime(converted_timestamp, '%Y-%m-%dT%H:%M:%SZ').strftime('%d-%m-%Y')
print (reformatted_timestamp)
# output
28-07-2019

这仍然不能解决您的原始问题,该问题与JSON日期和列日期有时不同有关.但是,这是我目前与发生日期差异有关的假设.

  1. root.App.main 中提取的 json 日期(fmt和raw)在世界标准时间(UTC)中.这很清楚,因为 raw 中的UNIX时间戳.

  1. The json date (fmt and raw) that are being extracted from root.App.main are in Coordinated Universal Time (UTC). This is clear because of the UNIX timestamp in raw.

在表格列中显示的日期似乎在东部标准时间(EST)时区. EST当前为UTC-4.这意味着美国东部时间2019-07-28 22:00(10pm)将是世界标准时间2019-07-29 02:00(2am).根据 traceroute ,托管 finance.yahoo.com 的服务器位于美国 结果.这些值也位于 json 文件中:

The dates being displayed in the table columns seem to be in the Eastern Standard Time (EST) timezone. EST is currently UTC-4. Which means that 2019-07-28 22:00 (10pm) EST would be 2019-07-29 02:00 (2am) UTC. The server hosting finance.yahoo.com looks to be in the United States, based on the traceroute results. These values are also in the json file:

  • 'exchangeTimezoneName':'America/New_York'
  • 'exchangeTimezoneShortName':'EDT'

还有一些日期差异可能链接到该网站使用的基础 React 代码.由于该代码不可见,因此很难诊断此问题.

There is also the possibility that some of the date differences are linked to the underlying React code, which the site uses. This issue is harder to diagnose, because the code isn't visible.

这时候,我相信最好的解决方案是使用UNIX时间戳作为您的真实时间参考.此引用可用于替换表格列的日期.

At this time I believe that the best solution would be to use the UNIX timestamp as your ground truth time reference. This reference could be used to replace the table column's date.

在JSON文件和列之间肯定存在某种类型的转换.

There is definitely some type of conversion happening between the JSON file and the columns.

NVIDIA JSON FILE:'endDate':{'raw':1561766400,'fmt':'2019-06-29'}

NVIDIA JSON FILE: 'endDate': {'raw': 1561766400, 'fmt': '2019-06-29'}

NVIDIA关联总收入列:2019/6/30

NVIDIA Associated Total Revenue column: 6/30/2019

总收入列日期应为6/28/2019(EDT),因为 1561766400 的UNIX时间戳为06/29/2019 12:00 am(UTC).

BUT the Total Revenue column date should be 6/28/2019 (EDT), because the UNIX time stamp for 1561766400 is 06/29/2019 12:00am (UTC).

DELL 的差异大于基本的UNIX时间戳和EDT时间戳转换.

The disparity with DELL is greater than a basic UNIX timestamp and a EDT timestamp conversion.

Dell JSON FILE:{"raw":1564704000,"fmt":"2019-08-02"}

DELL JSON FILE:{"raw":1564704000,"fmt":"2019-08-02"}

DELL关联总收入列:2019/7/31

DELL Associated Total Revenue column: 7/31/2019

如果将UNIX时间戳转换为EDT时间戳,结果将为8/1/2019,但在DELL示例中则不是这种情况,即7/31/2019. Yahoo代码库中的某些内容必须引起这种差异.

If we convert the UNIX timestamp to an EDT timestamp, the result would be 8/1/2019, but that is not the case in the DELL example, which is 7/31/2019. Something within the Yahoo code base has to be causing this difference.

我开始相信反应可能是造成这些日期差异的罪魁祸首,但我不能确定是否需要做更多的研究.

I'm starting to believe that React might be the culprit with these date differences, but I cannot be sure without doing more research.

如果React是根本原因,那么最好的选择就是使用JSON数据中的日期元素.

更新后的答案10-17-2019

这个问题非常有趣,因为这些列日期似乎与公司的财政季度末相关,而不是日期对话问题.

This problem is very interesting, because it seems that these column dates are linked to a company's official end of fiscal quarter and not a date conversation issue.

以下是

  • Apple Inc.(AAPL)
  • Atlassian Corporation Plc(TEAM)
  • Arrowhead Pharmaceuticals,Inc.(ARWR):

其列日期为:

  • 6/30/2019
  • 2019年3月31日
  • 12/31/2018
  • 9/30/2018

这些日期与这些财政季度相匹配.

These dates match to these fiscal quarters.

  • 第一季度(Q1):1月1日至3月31日.
  • 第二季度(Q2):4月1日至6月30日.
  • 第3季度(第3季度):7月1日至9月30日.
  • 第4季度(Q4):10月1日至12月31日

如本DELL示例所示,这些会计季度结束日期可能会有很大差异.

These fiscal quarter end dates can vary greatly as this DELL example shows.

Dell (已发布在纳斯达克) 财政季度末:2019年7月

DELL (posted in NASDAQ) End of fiscal quarter: July 2019

雅虎财经 列日期:7/31/2019

Yahoo Finance Column date: 7/31/2019

JSON日期:2019-08-02

JSON date: 2019-08-02

在公司的网站上:

戴尔技术公司的会计年度何时结束?

When does Dell Technologies’ fiscal year end?

  • 我们的会计年度为52周或53周,至最近的1月31日星期五结束.我们的2020会计年度将在2020年1月31日结束.有关以前的会计年度,请参见以下列表:我们的2019会计年度于2019年2月1日我们的2018财年截止于2018年2月2日我们的2017财年截止于2017年2月3日我们的2016财年截止于2016年1月29日我们的2015财年截止于2015年1月30日我们的2014财年截止于2014年1月31日,我们的2013财年于2013年2月1日结束
  • 注意::日期为05-03-19和08-02-19.

    NOTE: The 05-03-19 and 08-02-19 dates.

    这些来自DELL的JSON季度数据:

    These are from the JSON quarter data for DELL:

    • {'raw':1564704000,'fmt':'2019-08-02'}
    • {'raw':1556841600,'fmt':'2019-05-03'}

    这些列日期似乎与公司的财政季度结束日期相关联.因此,我建议您使用JSON日期作为主要参考元素或相应的列日期.

    P.S.雅虎发生某种日期伏都教,因为他们似乎根据假期,周末和月底移动这些列季度日期.

    P.S. There is some type of date voodoo occurring at Yahoo, because they seem to move these column quarter dates based on holidays, weekends and end of month.

    这篇关于无法使用请求解析网页的确切结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆