BeautifulSoup不接受完整的HTML代码

2024-06-01 09:30:56 发布

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

我在这段代码中遇到了一些问题,我试图从pokedex.org中获取所有口袋妖怪的名字。我的原始代码如下:

import requests
from bs4 import BeautifulSoup

url = 'https://pokedex.org/'
html = BeautifulSoup(requests.get(url).content,'lxml')

uls = html.find('ul', attrs = {'id':'monsters-list'})

print(uls.prettify())

然后,uls应该包含一些<li></li>,它们本身包含<span></span>,名称被包装在其中。它可以很好地获取前100个口袋妖怪的所有内容,但是对于其他500个口袋妖怪,它会返回空的<li></li>。我尝试了不同的解析器,如html.parserhtml5liblxml,但它没有改变任何东西


Tags: 代码fromorgimporturlhtmlli名字
2条回答

页面是动态加载的,因此requests不支持它。我们可以使用Selenium作为刮取页面的替代方法,并且还需要向下滚动页面

安装时使用:pip install selenium

here下载正确的ChromeDriver。以下是代码:

from bs4 import BeautifulSoup
from selenium import webdriver
import time

url = 'https://pokedex.org/'
webdriver = webdriver.Chrome()
webdriver.get(url)
time.sleep(2)

webdriver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(5)
html = BeautifulSoup(webdriver.page_source,'lxml')

uls = html.find('ul', attrs = {'id':'monsters-list'})

print(uls.prettify())

输出最后一项:

<li style="background: linear-gradient(90deg, #B8B8D0 50%, #A8B820 50%)">
  <button class="monster-sprite sprite-649" type="button">
  </button>
  <span>
   Genesect
  </span>
 </li>

看起来元素是由JavaScript创建的,但请求无法处理JavaScript动态生成的元素。 (如果我错了,请纠正我)

我建议使用selenium和ChromeWebDriver来获取页面源代码, 然后可以使用BeautifulSoup进行解析

(假设您使用chrome浏览器)

  1. 访问:chrome://settings/help并检查您的chrome版本
  2. 从官方网站下载chromewebdriver的合适版本 (https://chromedriver.chromium.org/downloads
  3. 将chromedriver.exe和python文件放在同一目录中

最后我们来看看代码

import requests
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

# headless background execution
Options = Options()
Options.headless = True

url = "https://pokedex.org/"
browser = webdriver.Chrome(options=Options)
browser.get(url)

html = BeautifulSoup(requests.get(url).content, 'lxml')
uls = html.find('ul', attrs={'id': 'monsters-list'})

print(uls.prettify())

相关问题 更多 >