该网站是否可与BeautifulSoup一起使用?

问题描述 投票:0回答:2

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

更确切地说,我想用div收集id = 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_tableNone对象。硒是解决方案吗?

enter image description here

python python-3.x selenium web-scraping beautifulsoup
2个回答
0
投票

正确的呼叫方式是:

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

0
投票

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

然后您得到那张桌子。

© www.soinside.com 2019 - 2024. All rights reserved.