Python 使用beautifulsoup对aria标签进行数据抓取
从以下内容中,我试图提取分析师的价格目标。 我对aria标签中的信息感兴趣 我使用以下设置尝试了联机找到的多个版本的Python 使用beautifulsoup对aria标签进行数据抓取,python,html,web-scraping,beautifulsoup,Python,Html,Web Scraping,Beautifulsoup,从以下内容中,我试图提取分析师的价格目标。 我对aria标签中的信息感兴趣 我使用以下设置尝试了联机找到的多个版本的BeautifulSoup: import requests from bs4 import BeautifulSoup headers = {'User-Agent' : 'XXXXX'} >> XXXXX replaced with actual url = 'https://finance.yahoo.com/quote/AAPL/analysis?p=AAPL'
BeautifulSoup
:
import requests
from bs4 import BeautifulSoup
headers = {'User-Agent' : 'XXXXX'} >> XXXXX replaced with actual
url = 'https://finance.yahoo.com/quote/AAPL/analysis?p=AAPL'
r = requests.get(url, headers=headers)
soup = BeautifulSoup(r.text, 'html.parser')
'div'
和'class'
之间,因此我尝试了以下方法:
target = soup.find('div', {'class':'Px(10px)'})
target = soup.find('section', attrs={'data-test':'price-targets'})
target = soup.find('div', {'class':'Px(10px)'})
target = soup.find('section', attrs={'data-test':'price-targets'})
target = soup.find('div', {'id':'mrt-node-Col2-5-QuoteModule'}).find_all('div')[0]
因此,我越来越接近选项3,但当我修改find_all
div索引时收到一个错误
提取aria标签中的4个数据是否有任何解决方案或周转
我的目标是'Low'
,'Current'
,'Average'
和'High'
旁边的数字
正如@Ann Zen在评论中提到的,该网站正在动态呈现元素和数据,Beautifulsoup
无法单独使用Selenium来处理它,Selenium
将等待应用程序加载后再尝试获取元素
示例由于selenium
可能需要花费时间进行迭代,我找到了第二种可能的解决方案,即使用请求
获取页面的源代码,并使用json和regex组合搜索数据。您能发布URL吗?感谢您编辑我的第一篇帖子!我发布了URL,您需要使用selenium
,因为您尝试获取的元素是动态的,Beautifulsoup
无法处理动态html。请使用selenium给出一个解决方案:)@AnnZen我正在使用:-)@utpaldut非常感谢您提供的信息!我将开始学习硒,同时等待您的解决方案:)