无法从 Morningstar 抓取 dataid - 如何从 Python 访问网络检查工具?
Cannot scrape dataid from Morningstar - How can I access the Network inspection tool from Python?
我正在尝试抓取 Morningstar.com 以获取网站上提供的每只基金的财务数据和价格。幸运的是,我在抓取财务数据(持股、资产配置、投资组合、风险等)方面没有问题,但是当要找到 URL 以 JSON 格式承载每个基金的每日价格时,有一个“dataid”值在 HTML 代码中不可用,没有它就无法知道承载所有价格的确切 URL。
我尝试将整个页面打印为许多基金的文本,其中 none 在 HTML 代码中显示了我获取价格所需的“dataid”值.托管价格的 URL 还包括“secid”,它很容易被抓取,但与我需要抓取的“dataid”完全没有关系。
import requests
from lxml import html
import re
import json
quote_page = "https://www.morningstar.com/etfs/arcx/aadr/quote.html"
prices1 = "https://mschart.morningstar.com/chartweb/defaultChart?type=getcc&secids="
prices2 = "&dataid="
prices3 = "&startdate="
prices4 = "&enddate="
starting_date = "2018-01-01"
ending_date = "2018-12-28"
quote_html = requests.get(quote_page, timeout=10)
quote_tree = html.fromstring(quote_html.text)
security_id = re.findall('''meta name=['"]secId['"]\s*content=['"](.*?)['"]''', quote_html.text)[0]
security_type = re.findall('''meta name=['"]securityType['"]\s*content=['"](.*?)['"]''', quote_html.text)[0]
data_id = "8225"
daily_prices_url = prices1 + security_id + ";" + security_type + prices2 + data_id + prices3 + starting_date + prices4 + ending_date
daily_prices_html = requests.get(daily_prices_url, timeout=10)
json_prices = daily_prices_html.json()
for json_price in json_prices["data"]["r"]:
j_prices = json_price["t"]
for j_price in j_prices:
daily_prices = j_price["d"]
for daily_price in daily_prices:
print(daily_price["i"] + " || " + daily_price["v"])
上面的代码只适用于“AADR”ETF,因为我在“data_id”变量中手动复制并粘贴了“dataid”值,没有这条信息就无法访问每日价格。我不想使用 Selenium 作为查找“dataid”的替代方法,因为它是一个非常慢的工具,而且我的目的是为超过 28k 的资金抓取数据,所以我只尝试了机器人网络抓取方法。
您对如何访问网络检查工具有什么建议吗,这是迄今为止我发现的唯一显示“dataid”的来源?
提前致谢
数据id可能没那么重要。我改变了与 AADR 关联的代码 F00000412E,同时保持数据 ID 不变。
我从这里得到了所有这些代码的列表:
https://www.firstrade.com/scripts/free_etfs/io.php
然后将选择的代码添加到您的 url 例如
[
"AIA",
"iShares Asia 50 ETF",
"FOUSA06MPQ"
]
使用FOUSA06MPQ
https://mschart.morningstar.com/chartweb/defaultChart?type=getcc&secids=FOUSA06MPQ;FE&dataid=8225&startdate=2017-01-01&enddate=2018-12-30
您可以通过将其他基金作为基准添加到您的图表中来验证这些值,例如XNAS:AIA
12 月 28 日的值为 55.32。将此与检索到的 JSON 进行比较:
我用
重复了这个
[
"ALD",
"WisdomTree Asia Local Debt ETF",
"F00000M8TW"
]
https://mschart.morningstar.com/chartweb/defaultChart?type=getcc&secids=F00000M8TW;FE&dataid=8225&startdate=2017-01-01&enddate=2018-12-30
无论安全性如何,dataId 8217 对我来说都很好。
我正在尝试抓取 Morningstar.com 以获取网站上提供的每只基金的财务数据和价格。幸运的是,我在抓取财务数据(持股、资产配置、投资组合、风险等)方面没有问题,但是当要找到 URL 以 JSON 格式承载每个基金的每日价格时,有一个“dataid”值在 HTML 代码中不可用,没有它就无法知道承载所有价格的确切 URL。
我尝试将整个页面打印为许多基金的文本,其中 none 在 HTML 代码中显示了我获取价格所需的“dataid”值.托管价格的 URL 还包括“secid”,它很容易被抓取,但与我需要抓取的“dataid”完全没有关系。
import requests
from lxml import html
import re
import json
quote_page = "https://www.morningstar.com/etfs/arcx/aadr/quote.html"
prices1 = "https://mschart.morningstar.com/chartweb/defaultChart?type=getcc&secids="
prices2 = "&dataid="
prices3 = "&startdate="
prices4 = "&enddate="
starting_date = "2018-01-01"
ending_date = "2018-12-28"
quote_html = requests.get(quote_page, timeout=10)
quote_tree = html.fromstring(quote_html.text)
security_id = re.findall('''meta name=['"]secId['"]\s*content=['"](.*?)['"]''', quote_html.text)[0]
security_type = re.findall('''meta name=['"]securityType['"]\s*content=['"](.*?)['"]''', quote_html.text)[0]
data_id = "8225"
daily_prices_url = prices1 + security_id + ";" + security_type + prices2 + data_id + prices3 + starting_date + prices4 + ending_date
daily_prices_html = requests.get(daily_prices_url, timeout=10)
json_prices = daily_prices_html.json()
for json_price in json_prices["data"]["r"]:
j_prices = json_price["t"]
for j_price in j_prices:
daily_prices = j_price["d"]
for daily_price in daily_prices:
print(daily_price["i"] + " || " + daily_price["v"])
上面的代码只适用于“AADR”ETF,因为我在“data_id”变量中手动复制并粘贴了“dataid”值,没有这条信息就无法访问每日价格。我不想使用 Selenium 作为查找“dataid”的替代方法,因为它是一个非常慢的工具,而且我的目的是为超过 28k 的资金抓取数据,所以我只尝试了机器人网络抓取方法。 您对如何访问网络检查工具有什么建议吗,这是迄今为止我发现的唯一显示“dataid”的来源? 提前致谢
数据id可能没那么重要。我改变了与 AADR 关联的代码 F00000412E,同时保持数据 ID 不变。
我从这里得到了所有这些代码的列表:
https://www.firstrade.com/scripts/free_etfs/io.php
然后将选择的代码添加到您的 url 例如
[
"AIA",
"iShares Asia 50 ETF",
"FOUSA06MPQ"
]
使用FOUSA06MPQ
https://mschart.morningstar.com/chartweb/defaultChart?type=getcc&secids=FOUSA06MPQ;FE&dataid=8225&startdate=2017-01-01&enddate=2018-12-30
您可以通过将其他基金作为基准添加到您的图表中来验证这些值,例如XNAS:AIA
12 月 28 日的值为 55.32。将此与检索到的 JSON 进行比较:
我用
重复了这个[
"ALD",
"WisdomTree Asia Local Debt ETF",
"F00000M8TW"
]
https://mschart.morningstar.com/chartweb/defaultChart?type=getcc&secids=F00000M8TW;FE&dataid=8225&startdate=2017-01-01&enddate=2018-12-30
无论安全性如何,dataId 8217 对我来说都很好。