gpt4 book ai didi

python-2.7 - 如何使用 Pandas read_html 和请求库来读取表?

转载 作者:行者123 更新时间:2023-12-04 04:34:29 24 4
gpt4 key购买 nike

我怎样才能刮取基金的价格:

http://www.prudential.com.hk/PruServlet?module=fund&purpose=searchHistFund&fundCd=JAS_U

这是错误的,但我如何修改它:

import pandas as pd
import requests
import re
url = 'http://www.prudential.com.hk/PruServlet?module=fund&purpose=searchHistFund&fundCd=JAS_U'
tables = pd.read_html(requests.get(url).text, attrs={"class":re.compile("fundPriceCell\d+")})

最佳答案

我喜欢用 lxml 来解析和查询 HTML。这是我想出的:

import requests
from lxml import etree

url = 'http://www.prudential.com.hk/PruServlet?module=fund&purpose=searchHistFund&fundCd=JAS_U'
doc = requests.get(url)
tree = etree.HTML(doc.content)

row_xpath = '//tr[contains(td[1]/@class, "fundPriceCell")]'

rows = tree.xpath(row_xpath)

for row in rows:
(date_string, v1, v2) = (td.text for td in row.getchildren())
print "%s - %s - %s" % (date_string, v1, v2)

关于python-2.7 - 如何使用 Pandas read_html 和请求库来读取表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19983406/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com