Python 请求-HTML - 找不到特定数据
Python Requests-HTML - Can't find specific data
我正在尝试使用 python 请求-html 库来抓取网页。
link 该网页是 https://www.koyfin.com/charts/g/USADebt2GDP?view=table ,
下图显示(红色四舍五入的数据)我想要得到的数据。
我的代码是这样的,
from requests_html import HTMLSession
session = HTMLSession()
r = session.get('https://www.koyfin.com/charts/g/USADebt2GDP?view=table')
r.html.render(timeout=60)
print(r.text)
网页html这样,
问题是当我抓取网页时我找不到我想要的数据,在 HTML 代码中我可以看到
正文部分第一个 div 标签内的数据。
关于如何解决这个问题的任何具体建议。
谢谢。
问题是初始页面加载后 JavaScript 代码正在加载数据。一种解决方案是使用 Selenium
驱动网络浏览器抓取页面。但是使用常规浏览器,我查看了正在发出的网络请求,看起来您正在寻找的数据正在通过以下 AJAX 调用加载:
https://api.koyfin.com/api/v2/commands/g/g.gec/USADebt2GDP?dateFrom=2010-08-20&dateTo=2020-09-05&period=yearly
所以:
import requests
response = requests.get('https://api.koyfin.com/api/v2/commands/g/g.gec/USADebt2GDP?dateFrom=2010-08-20&dateTo=2020-09-05&period=yearly')
results = response.json();
print(results)
for t in results['graph']['data']:
print(t)
打印:
{'ticker': 'USADebt2GDP', 'companyName': 'United States Gross Federal Debt to GDP', 'startDate': '1940-12-31T00:00:00.000Z', 'endDate': '2019-12-31T00:00:00.000Z', 'unit': 'percent', 'graph': {'column_names': ['Date', 'Volume'], 'data': [['2010-12-31', 91.4], ['2011-12-31', 96], ['2012-12-31', 100.1], ['2013-12-31', 101.2], ['2014-12-31', 103.2], ['2015-12-31', 100.8], ['2016-12-31', 105.8], ['2017-12-31', 105.4], ['2018-12-31', 106.1], ['2019-12-31', 106.9]]}, 'withoutLiveData': True}
['2010-12-31', 91.4]
['2011-12-31', 96]
['2012-12-31', 100.1]
['2013-12-31', 101.2]
['2014-12-31', 103.2]
['2015-12-31', 100.8]
['2016-12-31', 105.8]
['2017-12-31', 105.4]
['2018-12-31', 106.1]
['2019-12-31', 106.9]
我是如何想到 URL
当您点击最后一条消息时:
我正在尝试使用 python 请求-html 库来抓取网页。
link 该网页是 https://www.koyfin.com/charts/g/USADebt2GDP?view=table ,
下图显示(红色四舍五入的数据)我想要得到的数据。
我的代码是这样的,
from requests_html import HTMLSession
session = HTMLSession()
r = session.get('https://www.koyfin.com/charts/g/USADebt2GDP?view=table')
r.html.render(timeout=60)
print(r.text)
网页html这样,
问题是当我抓取网页时我找不到我想要的数据,在 HTML 代码中我可以看到 正文部分第一个 div 标签内的数据。 关于如何解决这个问题的任何具体建议。
谢谢。
问题是初始页面加载后 JavaScript 代码正在加载数据。一种解决方案是使用 Selenium
驱动网络浏览器抓取页面。但是使用常规浏览器,我查看了正在发出的网络请求,看起来您正在寻找的数据正在通过以下 AJAX 调用加载:
https://api.koyfin.com/api/v2/commands/g/g.gec/USADebt2GDP?dateFrom=2010-08-20&dateTo=2020-09-05&period=yearly
所以:
import requests
response = requests.get('https://api.koyfin.com/api/v2/commands/g/g.gec/USADebt2GDP?dateFrom=2010-08-20&dateTo=2020-09-05&period=yearly')
results = response.json();
print(results)
for t in results['graph']['data']:
print(t)
打印:
{'ticker': 'USADebt2GDP', 'companyName': 'United States Gross Federal Debt to GDP', 'startDate': '1940-12-31T00:00:00.000Z', 'endDate': '2019-12-31T00:00:00.000Z', 'unit': 'percent', 'graph': {'column_names': ['Date', 'Volume'], 'data': [['2010-12-31', 91.4], ['2011-12-31', 96], ['2012-12-31', 100.1], ['2013-12-31', 101.2], ['2014-12-31', 103.2], ['2015-12-31', 100.8], ['2016-12-31', 105.8], ['2017-12-31', 105.4], ['2018-12-31', 106.1], ['2019-12-31', 106.9]]}, 'withoutLiveData': True}
['2010-12-31', 91.4]
['2011-12-31', 96]
['2012-12-31', 100.1]
['2013-12-31', 101.2]
['2014-12-31', 103.2]
['2015-12-31', 100.8]
['2016-12-31', 105.8]
['2017-12-31', 105.4]
['2018-12-31', 106.1]
['2019-12-31', 106.9]
我是如何想到 URL
当您点击最后一条消息时: