【问题标题】:Is this website scrape-able with BeautifulSoup?这个网站可以用 BeautifulSoup 抓取吗?
【发布时间】:2020-02-05 16:37:05
【问题描述】:

我想抓取这个网站:https://www.projets-environnement.gouv.fr/pages/home/

更准确地说,我想收集divid = table-wrapper 中的表。

我的问题是我无法用BeautifulSoup 捕捉到它。

这是我的代码:

url = 'https://www.projets-environnement.gouv.fr/pages/home/'
html = requests.get(url).text
soup = BeautifulSoup(html, "html5lib")
div_table = soup.findAll('div', id_='table-wrapper')

但是div_table 是一个None 对象。 硒是解决方案吗?

【问题讨论】:

  • 你确定网站上有一个带有id=table_wrapper的div吗?
  • 是的,只是一个小错误,我将帖子从id='table_wraper' 编辑为id='table-wrapper'
  • 当我查看网站时,我找不到任何带有table-wrappertable_wrapper 的div
  • 我又查了一遍,在帖子里加了一张图片。
  • 到底是什么问题?如果你需要的内容是动态生成的,那就用Selenium,否则你可以坚持Requests和BeautifulSoup。

标签: python python-3.x selenium web-scraping beautifulsoup


【解决方案1】:

我认为你应该使用selenium:

from bs4 import BeautifulSoup

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.options import Options

url = 'https://www.projets-environnement.gouv.fr/pages/home/'

options = Options()
options.headless = True
driver = webdriver.Firefox(firefox_options=options)
driver.get(url)

html = driver.page_source
soup = BeautifulSoup(html, "html.parser")
mytable = soup.find('div', id='table-wrapper')

然后你就得到了那张桌子。

【讨论】:

    【解决方案2】:

    正确的调用方式是:

    soup.find("div", {"id": "table-wrapper"})
    

    【讨论】:

      猜你喜欢
      • 2021-01-25
      • 1970-01-01
      • 2020-06-27
      • 2019-12-15
      • 1970-01-01
      • 1970-01-01
      • 2020-05-30
      • 1970-01-01
      相关资源
      最近更新 更多