개발로그/기타
[크롤링] Yahoo Finance 404 Error
Life4IoT.rnd
2022. 3. 31. 20:00
반응형
BeautifulSoup을 이용해 Yahoo Finance 데이터를 수신하려고 하는데 아래와 같이 404 에러가 발생 했다.
ticker = 'AAPL'
URL = 'https://finance.yahoo.com/quote/{0}/key-statistics?p={0}'.format(ticker)
response = requests.get(URL)
if response.status_code == 200:
html = response.text
soup = BeautifulSoup(html, 'lxml')
print(soup)
else:
print(response.status_code)
$ python3 test.py
404
코드를 아래와 같이 수정하여 위 문제를 해결 함.
ticker = 'AAPL'
URL = 'https://finance.yahoo.com/quote/{0}/key-statistics?p={0}'.format(ticker)
html = BeautifulSoup(requests.get(URL,
headers={'User-agent': 'Mozilla/5.0'}).text, "lxml")
print(html)
반응형