python - 如何从
元素“id”
问题描述
我正在学习如何刮,然后我不是很先进。我从彭博社的公司描述中摘录下来。例如从这个页面(https://www.bloomberg.com/research/stocks/private/snapshot.asp?privcapId=320105)
我想刮
<p id="bDescTeaser" itemprop="description">Fiat Chrysler Automobiles N.V., ...</p>
我的脚本:
from urllib.request import Request, urlopen
from bs4 import BeautifulSoup
html= 'https://www.bloomberg.com/research/stocks/private/snapshot.asp?
privcapId=32010'
page = urlopen(html)
data = BeautifulSoup(page, 'html.parser')
text = data.find('p',id="bDescTeaser",itemprop=" ")
print(text.get_text))
如果我尝试运行我得到的程序,
AttributeError: 'NoneType' object has no attribute 'get_text'
这是我的代码还是这个特定的 webapge 的问题?
解决方案
在您的解决方案中,彭博阻止了您的请求。因为它认为你是一个机器人。您应该使用请求库并将用户代理作为标头发送。您将通过这种方式获得预期的输出。
import requests
from bs4 import BeautifulSoup
header = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0'}
request = requests.get('https://www.bloomberg.com/research/stocks/private/snapshot.asp?privcapId=320105',headers=header)
soup = BeautifulSoup(request.text, 'html.parser')
text = soup.find('p',id="bDescTeaser")
print(text.get_text())
推荐阅读
- nunit - [Retry] 和 [Repeat] 属性如何在 NUnit 中交互
- android - Google Nearby Connection:文件交换不会传输整个文件
- unity3d - 不太明白四元数旋转(欧拉角)
- r - 使用 dplyr 在特定列值的第一个实例之后过滤 R 中数据框中的行
- assembly - 我怎样才能画出一个直立的星号三角形?
- c# - 如何检测用户何时从 Visual Studio 运行或何时是常规运行服务
- ubuntu - pandoc:如何设置几何驱动程序
- html - 悬停时如何重置/控制转换延迟?
- java - 如何仅捕获可见通知?安卓
- angular - Angular Reactive Forms Select Option 预选默认值不适用于对象