【问题标题】:BeautifulSoup find class return noneBeautifulSoup 查找类返回无
【发布时间】:2020-08-02 10:15:45
【问题描述】:

我正在使用BeautifulSoup 和 python 编写代码来从网站上抓取信息,在我尝试按“类”类别获取特定内容后,它返回“[]”,这是否意味着“无”?

这是否意味着它没有什么可刮的?

以下是我的代码:

import requests
from bs4 import BeautifulSoup

page = requests.get('https://www.metservice.com/towns-cities/locations/auckland/7-days')
soup = BeautifulSoup(page.content, 'html.parser')
week = soup.find_all(class_='IconWithText-content')

print(week)

【问题讨论】:

  • 很奇怪,我从网站上找到了包含您需要的文本的类,但它没有出现在 response.text 中,可能是防止网络抓取,但我没有看到为什么气象服务会这样做。
  • 是的,我刚刚通过使用亚马逊网站的示例从教程中学到了它,它工作得很好,我很惊讶看到气象服务这样做。

标签: python selenium beautifulsoup


【解决方案1】:

问题是当您尝试抓取时页面内容未加载,

您可以将seleniumBeautifulSoup 一起使用

例子

import time
from bs4 import BeautifulSoup
from selenium import webdriver

url = "https://www.metservice.com/towns-cities/locations/auckland/7-days"
browser = webdriver.Firefox()
browser.get(url)
time.sleep(5)
html = browser.page_source
soup = BeautifulSoup(html, 'html.parser')
week = soup.find_all(class_='IconWithText-content')
print(week)

【讨论】:

  • 我想是因为你说的,内容是Javascript,不能用html方法报废,我用过你上面的代码,但没那么简单,我会google并学习如何安装和其他工作人员让硒工作。谢谢,不胜感激。
  • @D1ag0 没问题,这个网站上有很多例子,如果遇到问题,只需发布​​新问题-祝你好运,玩得开心
【解决方案2】:

selenium 是个好主意。但是,如果你想要好的用户界面,你可以从网上下载文件,阅读它,然后像这样删除它:

import wget
from bs4 import BeautifulSoup
import os

wget.download('https://www.metservice.com/towns-cities/locations/auckland/7-days', "tmp.html")
with open('tmp.html') as week_html:
    soup = BeautifulSoup(week_html, 'html.parser')
os.remove('tmp.html')
week = soup.find_all(class_='IconWithText-content')

print(week)

这应该可行。

【讨论】:

    【解决方案3】:

    如果您查看该请求返回的实际 html,您会发现其中没有包含 classIconWithText-content 的元素,因此您找不到它。

    你所做的会起作用,或者这样:

    soup.find_all(attrs={'class': 'IconWithText-content'})
    

    【讨论】:

    • 谢谢,但我的问题可能是我仍然对上面的建议代码有问题。感谢您的帮助。
    猜你喜欢
    • 2017-04-15
    • 2019-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-20
    • 2018-05-24
    • 1970-01-01
    • 2020-10-11
    相关资源
    最近更新 更多