这个网站可以用BeautifulSoup刮吗?

2024-05-23 19:11:13 发布

您现在位置:Python中文网/ 问答频道 /正文

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

更准确地说,我想用id = table-wrapper收集div中的表

我的问题是,我不能用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对象。 硒是解决方案吗

enter image description here


Tags: httpsdividurlhomehtmlwwwtable
2条回答

正确的呼叫方式是:

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

我认为你应该使用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')

你得到那张桌子

相关问题 更多 >