python - 网页抓取 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,但它一直显示索引超出范围。
有没有办法获得多个股票行情,而不是一次只做一个股票行情?谢谢!
解决方案
推荐阅读
- ios - SwiftUI:如何在不使用几何阅读器的情况下根据框架的大小计算视图的大小?
- dependency-injection - blazor wasm 在 program.cs Main 中注入 IJSRuntime
- c# - 两个不同的类是否有可能具有一个属性——一个类的集合,并且每个对象都知道它属于前两个类中的哪一个?
- google-sheets - 是否可以将多张工作表中的 Google 表格列堆叠到一份报告中?
- python-3.x - 父类值的继承
- python - 与 Numpy 相比优化 Cython 循环
- javascript - React-localstorage not available in another component unless i refresh page
- typescript - 禁止显式类型参数或约束联合类型参数以提高类型安全性
- firebase - 显示 Firebase 用户喜欢
- spring - spring boot如何在html文件中添加css、js等静态文件?