这个网站可以与BeautifulSoup一起使用吗?

时间:2020-02-05 16:37:05

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

我想抓这个网站: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

2 个答案:

答案 0 :(得分: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')

然后您得到那个桌子。

答案 1 :(得分:0)

正确的呼叫方式是:

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