元组对象没有属性文本
Tuple object has no attribute text
我正在编写代码以从雅虎财经网站获取特定信息
page = requests.get('http://finance.yahoo.com/q/pr?s=%s')
tree=html.fromstring(page.text)
annual_report = tree.xpath('//td[@class="yfnc_datamoddata1"]/text()')
annual_report
其中 %s
是股票名称。如果我手动输入股票名称,一切都很好。但是,如果我尝试 运行 为我创建的列表创建一个 for 循环,
for x in my_list:
page = requests.get('http://finance.yahoo.com/q/pr?s=%s'),(x,)
tree=html.fromstring(page.text)
annual_report = tree.xpath('//td[@class="yfnc_datamoddata1"]/text()')
print annual_report
我在树线上遇到错误 'tuple' object has no attribute 'text'
page = requests.get('http://finance.yahoo.com/q/pr?s=%s'),(x,)
这不是定义格式字符串的方法。你不小心创建了一个元组 ('http://finance.yahoo.com/q/pr?s=%s', x)
要将 x
合并到字符串中,请这样写:
page = requests.get('http://finance.yahoo.com/q/pr?s=%s' % x)
甚至更好,因为不需要说明符:
page = requests.get('http://finance.yahoo.com/q/pr?s={0}'.format(x))
page = requests.get('http://finance.yahoo.com/q/pr?s=' + x)
你的错误是:
page = requests.get('http://finance.yahoo.com/q/pr?s=%s'),(x,)
您没有格式化字符串,而是 'page' 一个元组。
这应该有效:
page = requests.get('http://finance.yahoo.com/q/pr?s=%s' % (x,))
我正在编写代码以从雅虎财经网站获取特定信息
page = requests.get('http://finance.yahoo.com/q/pr?s=%s')
tree=html.fromstring(page.text)
annual_report = tree.xpath('//td[@class="yfnc_datamoddata1"]/text()')
annual_report
其中 %s
是股票名称。如果我手动输入股票名称,一切都很好。但是,如果我尝试 运行 为我创建的列表创建一个 for 循环,
for x in my_list:
page = requests.get('http://finance.yahoo.com/q/pr?s=%s'),(x,)
tree=html.fromstring(page.text)
annual_report = tree.xpath('//td[@class="yfnc_datamoddata1"]/text()')
print annual_report
我在树线上遇到错误 'tuple' object has no attribute 'text'
page = requests.get('http://finance.yahoo.com/q/pr?s=%s'),(x,)
这不是定义格式字符串的方法。你不小心创建了一个元组 ('http://finance.yahoo.com/q/pr?s=%s', x)
要将 x
合并到字符串中,请这样写:
page = requests.get('http://finance.yahoo.com/q/pr?s=%s' % x)
甚至更好,因为不需要说明符:
page = requests.get('http://finance.yahoo.com/q/pr?s={0}'.format(x))
page = requests.get('http://finance.yahoo.com/q/pr?s=' + x)
你的错误是:
page = requests.get('http://finance.yahoo.com/q/pr?s=%s'),(x,)
您没有格式化字符串,而是 'page' 一个元组。 这应该有效:
page = requests.get('http://finance.yahoo.com/q/pr?s=%s' % (x,))