Python 3.x urllib.error.HTTPError:HTTP错误404:使用request.urlopen()时未找到
我正在学习一个教程,当使用Python 3.x urllib.error.HTTPError:HTTP错误404:使用request.urlopen()时未找到,python-3.x,downloading-website-files,Python 3.x,Downloading Website Files,我正在学习一个教程,当使用request.urlopen(url)时,我遇到了一个错误,我尝试检查了url () 很好 这是我的密码: from urllib import request import datetime def download_stock_from_day_until_today(stock_code, start_date): current_day = datetime.date.today() formatted_current_day = datet
request.urlopen(url)
时,我遇到了一个错误,我尝试检查了url
()
很好
这是我的密码:
from urllib import request
import datetime
def download_stock_from_day_until_today(stock_code, start_date):
current_day = datetime.date.today()
formatted_current_day = datetime.date.strftime(current_day, "%m/%d/%Y") #formats today's date for links
#formatted url
url = "https://www.wsj.com/market-data/quotes/PH/XPHS/"+ stock_code +"/historical-prices/download?MOD_VIEW=page&num_rows=150&range_days=150&startDate="+ start_date +"&endDate=" + formatted_current_day
print(url)
response = request.urlopen(url) #requests the csv file
csv = response.read() #reads the csv file
csv_str = str(csv)
lines = csv_str.split("\\n")
dest_url = r'asd.csv'
fx = open(dest_url, "w")
for line in lines:
fx.write(line + "\n")
fx.close()
download_stock_from_day_until_today("JFC", "06/01/2020")
我在控制台中得到的错误是:
Traceback (most recent call last):
File "C:/Users/Lathrix/PycharmProject/StockExcelDownloader/main.py", line 23, in <module>
download_stock_from_day_until_today("JFC", "06/01/2020")
File "C:/Users/Lathrix/PycharmProject/StockExcelDownloader/main.py", line 12, in download_stock_from_day_until_today
response = request.urlopen(url) #requests the csv file
File "C:\Users\Lathrix\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "C:\Users\Lathrix\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 531, in open
response = meth(req, response)
File "C:\Users\Lathrix\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 640, in http_response
response = self.parent.error(
File "C:\Users\Lathrix\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 569, in error
return self._call_chain(*args)
File "C:\Users\Lathrix\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 502, in _call_chain
result = func(*args)
File "C:\Users\Lathrix\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 649, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 404: Not Found
回溯(最近一次呼叫最后一次):
文件“C:/Users/Lathrix/PycharmProject/StockExcelDownloader/main.py”,第23行,在
从即日至今日下载股票(“JFC”,“06/01/2020”)
文件“C:/Users/Lathrix/PycharmProject/StockExcelDownloader/main.py”,第12行,从下载到今天
response=request.urlopen(url)#请求csv文件
文件“C:\Users\Lathrix\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py”,第222行,在urlopen中
返回opener.open(url、数据、超时)
文件“C:\Users\Lathrix\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py”,第531行,打开
响应=方法(请求,响应)
http\U响应中的文件“C:\Users\Lathrix\AppData\Local\Programs\Python38-32\lib\urllib\request.py”,第640行
响应=self.parent.error(
文件“C:\Users\Lathrix\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py”第569行出错
返回自我。调用链(*args)
文件“C:\Users\Lathrix\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py”,第502行,在调用链中
结果=func(*args)
文件“C:\Users\Lathrix\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py”,第649行,默认为http\u error\u
raise HTTPError(请求完整的url、代码、消息、hdrs、fp)
urllib.error.HTTPError:HTTP错误404:未找到
看来wsj.com不喜欢urllib的用户代理。
排队
response = request.urlopen(request.Request(url,headers={'User-Agent': 'Mozilla/5.0'}))
你的代码工作正常你怎么知道网站不能使用它?有什么解决办法吗?@lathrix我运行了你的代码,看到它打印了正确的url,然后我试图url打开www.wsj,com,但出现了404错误。从浏览器上看,它工作正常,所以我怀疑是用户代理标题。解决办法在我的答案中。替换你就行了r第12行和我提供的那一行。