Webscraping Yahoo Finance w/ Python 和 BeautifulSoup -- NameError: Name not defined and Error 404

问题描述 投票:0回答:0

我正试图从雅虎财经抓取财务日期。我要抓取的两个页面是财务页面和关键统计页面。

到目前为止,我一直在使用以下代码:

import re
import json
import csv
from io import StringIO
from bs4 import BeautifulSoup
import requests

url_stats = 'https://ca.finance.yahoo.com/quote/{}/key-statistics?p={}'
url_financials = 'https://ca.finance.yahoo.com/quote/{}/financials?p={}'

headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/111.0'}
stock = 'MSFT'

response = requests.get(url_financials.format(stock, stock))
soup = BeautifulSoup(response.text, 'html.parser')

pattern = re.compile (r'\s--\sData\s--\s')

script_data = soup.find('script', text=pattern),contents[0]    

在最后一个条目之后:我收到以下错误:

警告(来自警告模块): 文件“”,第 1 行 弃用警告:不推荐使用 find() 类型方法的“文本”参数。请改用“字符串”。 追溯(最近一次通话): 文件“”,第 1 行,在 script_data = soup.find('script', text=pattern),内容[0] NameError:名称“内容”未定义

我在这里看到其他帖子提到使用标题。我确实尝试过,但我仍然遇到同样的错误。想知道我能做些什么来解决这个问题。

作为参考,我正在使用这个 YT 教程来抓取雅虎财经:https://www.youtube.com/watch?v=fw4gK-leExw

此代码过去在视频中以及最近由我自己使用时都有效,但我不确定为什么不再有效。

python web-scraping yahoo-finance
© www.soinside.com 2019 - 2024. All rights reserved.