首页 > 解决方案 > 网页抓取 Yahoo Industry

问题描述

我正在学习网络抓取。我能够从https://finance.yahoo.com/quote/AAPL?p=AAPL页面提取数据。但是当我尝试从个人资料页面https://finance.yahoo.com/quote/AAPL/profile?p=AAPL中提取时它不起作用。


import bs4 as bs
import requests

r = requests.get('https://finance.yahoo.com/quote/AAPL/profile?p=AAPL')
soup = bs.BeautifulSoup(r.content,'lxml',from_encoding='utf-8')

industry = soup.find_all('div',attrs={'id':'Main'})[0].find(attrs={'data-reactid':'25'}).get_text()

print(industry)

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-35-3edd92f273ed> in <module>
      5 soup = bs.BeautifulSoup(r.content,'lxml',from_encoding='utf-8')
      6 
----> 7 industry = soup.find_all('div',attrs={'id':'Main'})[0].find(attrs={'data-reactid':'25'}).get_text()
      8 
      9 print(industry)

IndexError: list index out of range

它应该得到消费电子产品。行业:消费电子

我不确定为什么它会超出范围,因为我点击了这个词本身的检查,然后找到一个包含消费电子产品的 reactid 的 id。我尝试了一些 id 列表,包括 Main,但它一直显示索引超出范围。

有没有办法获得多个股票行情,而不是一次只做一个股票行情?谢谢!

标签: pythonweb-scraping

解决方案


推荐阅读