gpt4 book ai didi

Python LXML.HMTL Xpath 返回空列表

转载 作者:太空宇宙 更新时间:2023-11-04 05:40:30 26 4
gpt4 key购买 nike

问题:

  • date_list 是一个空列表。

  • 不应为空,因为列表长度应等于 octfiling_type_list 的列表长度。

我做了什么:

  1. 搜索拼写错误。

  2. 尝试了不同的公司(例如 REXAHN PHARMACEUTICALS, INC)。

  3. 对 Xpath 参数使用了不同的词。

代码:

filing_dates_pg = requests.get('http://csuite.xbrl.us/php/dispatch.php?Task=xbrlValues&Element=NetCashProvidedByUsedInOperatingActivitiesContinuingOperations&CIK=0001228627&Ultimus=true&DimReqd=false&API_Key=xxxx')
# The URL easiest to read in FIREFOX Browser.

Datestree = html.fromstring(filing_dates_pg.text)
date_list = Datestree.xpath('//periodStart/text()')
filing_type_list = Datestree.xpath('//period/text()')
ocf = Datestree.xpath('//amount/text()')

print ocf # Works, list matches data in URL page
print filing_type_list # Works
print type(date_list)
print date_list # Does NOT work

输出

['-2977634', '-4566843', '-5146845', '-6986598', '-53917328', '-2425835', '-55897199', '-7490595', '-57546899', '-10651611', '-58620377', '-15530306', '-60171904', '-1916510', '-62675625', '-3896381', '-64356343', '-5546081', '-66605233', '-6619559', '-69181840', '-1551527', '-4055248', '-5735966', '-7984856', '-11041211', '-2576607', '-4038817', '-5527773', '-8441718', '-8129391', '-12944191']
['1H', '3QCUM', 'Y', 'Y', '2001-03-19 - 2012-04-01', '1Q', '2001-03-19 - 2012-07-01', '1H', '2001-03-19 - 2012-10-01', '3QCUM', '2001-03-19 - 2013-01-01', 'Y', '2001-03-19 - 2013-04-01', '1Q', '2001-03-19 - 2013-07-01', '1H', '2001-03-19 - 2013-10-01', '3QCUM', '2001-03-19 - 2014-01-01', 'Y', '2001-03-19 - 2014-04-01', '1Q', '1H', '3QCUM', 'Y', 'Y', '1Q', '1Q', '1H', '1H', '3QCUM', '3QCUM']
<type 'list'>
[]
ValueError: Length mismatch: Expected axis has 0 elements, new values have 1 elements

最佳答案

看起来您正在使用 lxml.html.fromstring 而不是 lxml.etree.fromstring

使用lxml.etree.fromstring。否则,您需要指定 //periodstart/text() xpath 表达式(全部小写),因为在 HTML 中标记名称不区分大小写。

from lxml import etree

...

Datestree = etree.fromstring(filing_dates_pg.text)

关于Python LXML.HMTL Xpath 返回空列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34102082/

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