Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 使用Python和Xpath从Web上抓取股票价格5天_Python 3.x_Xpath - Fatal编程技术网

Python 3.x 使用Python和Xpath从Web上抓取股票价格5天

Python 3.x 使用Python和Xpath从Web上抓取股票价格5天,python-3.x,xpath,Python 3.x,Xpath,我有以下代码可以从YahooFinance网站上删除某一股票的股价。我想在5天内删除这些数据。然而,输出给我的是刚开始的一天的股价,重复了5次 我如何修改代码以提供过去5天的股价输出 预期输出为(这是过去5天的实际数据) Python代码: import requests from lxml import html from datetime import datetime,timedelta start_url = "https://in.finance.yahoo.com/quo

我有以下代码可以从YahooFinance网站上删除某一股票的股价。我想在5天内删除这些数据。然而,输出给我的是刚开始的一天的股价,重复了5次

我如何修改代码以提供过去5天的股价输出

预期输出为(这是过去5天的实际数据)

Python代码:

import requests  
from lxml import html  
from datetime import datetime,timedelta  
start_url = "https://in.finance.yahoo.com/quote/HINDUNILVR.NS?p=HINDUNILVR.NS"  
start = datetime(2018,3,22)  
for _ in range(5):    
    url = start_url.format(start.strftime("%Y%m%d"))  
    start -= timedelta(days=1)  
    page = requests.get(url)  
    tree = html.fromstring(page.content)  
    price = tree.xpath('//*[@id="quote-summary"]/div[1]/table/tbody/tr[1]/td[2]/span/text()')  
    print(price)
['1,311.85']   
['1,311.85']  
['1,311.85']  
['1,311.85']  
['1,311.85']  
输出:

import requests  
from lxml import html  
from datetime import datetime,timedelta  
start_url = "https://in.finance.yahoo.com/quote/HINDUNILVR.NS?p=HINDUNILVR.NS"  
start = datetime(2018,3,22)  
for _ in range(5):    
    url = start_url.format(start.strftime("%Y%m%d"))  
    start -= timedelta(days=1)  
    page = requests.get(url)  
    tree = html.fromstring(page.content)  
    price = tree.xpath('//*[@id="quote-summary"]/div[1]/table/tbody/tr[1]/td[2]/span/text()')  
    print(price)
['1,311.85']   
['1,311.85']  
['1,311.85']  
['1,311.85']  
['1,311.85']  

您希望加载不同日期的网站。因此,您获取URL并尝试使用
str.format
追加日期:

start_url = "https://in.finance.yahoo.com/quote/HINDUNILVR.NS?p=HINDUNILVR.NS"
start = datetime(2018,3,22)
url = start_url.format(start.strftime("%Y%m%d"))
问题是,
格式
不知道将日期放在哪里,因为您没有定义占位符<代码>url在这三行之后如下所示:

>>> start_url = "https://in.finance.yahoo.com/quote/HINDUNILVR.NS?p=HINDUNILVR.NS"
>>> start = datetime(2018,3,22)
>>> start_url.format(start.strftime("%Y%m%d"))
'https://in.finance.yahoo.com/quote/HINDUNILVR.NS?p=HINDUNILVR.NS'
要将日期添加到URL,必须指定占位符:

>>> start_url = "https://in.finance.yahoo.com/quote/HINDUNILVR.NS?p=HINDUNILVR.NS{0}"
注意末尾的
{0}
。这是
format
将放置由
start.strftime(“%Y%m%d”)生成的字符串的位置。


把它放在那里没有意义,只是为了演示
格式如何工作。您需要相应地调整您的URL,例如添加一个
?date=
参数。

您是否查看了请求的URL?如果我没有弄错的话,您丢失了占位符,要用日期替换为
格式
。您能详细说明一下吗,我确实检查了URL,但它没有“日期”子字符串,因为有些URL需要它。在这种情况下,您建议我如何使用占位符。如何添加“?date=”参数在这种情况下,此站点不是由我控制的,对吗?对不起我的无知。我对这个很陌生。如果网站不允许你用日期参数调用它,就没有机会使用它。谢谢你的澄清