【问题标题】:web scraping using BeautifulSoup, why the output is none?使用 BeautifulSoup 抓取网页,为什么没有输出?
【发布时间】:2021-01-10 12:45:41
【问题描述】:

输出应该是:

标准普尔 500 指数

3,824.68

这里的项目链接: https://www.freecodecamp.org/news/how-to-scrape-websites-with-python-and-beautifulsoup-5946935d93fe/

import requests
from bs4 import BeautifulSoup
import ssl
url = "https://www.bloomberg.com/quote/SPX:IND"
html = requests.get(url)
soup = BeautifulSoup(html.content, "html.parser")
name_box = soup.find("h1", attrs={"class": "name"})
name = str(name_box)
print(name)
price_box = soup.find("div", attrs={"class": "price"})
price = str(price_box)
print(price)

【问题讨论】:

    标签: web-scraping beautifulsoup python-3.8


    【解决方案1】:

    会发生什么?

    您会得到输出 None,因为您不会在响应中找到预期的数据。

    检查回复,您将获得以下信息:

    我们从您的计算机网络中检测到异常活动。到 继续,请单击下面的框让我们知道您不是 机器人。

    可以看看这个alternative

    【讨论】:

      【解决方案2】:

      HedgeHog 是正确的,请求页面存在问题。以下是对程序抓取部分的一些修复

      name_box = soup.find("h1", attrs={"class": "companyName__99a4824b"})
      name = name_box.text
      print(name)
      price_box = soup.find("span", attrs={"class": "priceText__1853e8a5"})
      price = price_box.text
      print(price)
      

      【讨论】:

      • 它发送了这个错误:name = name_box.text AttributeError: 'NoneType' object has no attribute 'text'
      • 您是否已经解决了彭博因异常活动而阻止您的问题?如果没有,那么可以查看这篇文章以获取彭博股票数据:stackoverflow.com/a/58070435。数据是否必须来自彭博社?你也可以从谷歌上抓取
      猜你喜欢
      • 2019-09-25
      • 1970-01-01
      • 2021-08-08
      • 1970-01-01
      • 2020-09-17
      • 1970-01-01
      • 1970-01-01
      • 2014-08-16
      相关资源
      最近更新 更多