Python 解析使用 BeatifulSoup 错误

Python parsing using BeatifulSoup error

当我运行这段代码时,我得到一个错误 汤 = BeautifulSoup(来源,"lxml") 类型错误:'module' 对象不可调用

from selenium import webdriver
import bs4 as BeautifulSoup

def html_pin():
    browser = webdriver.Chrome()
    browser.get('http://FULL_URL')
    sources = browser.page_source
    browser.quit()
    soup = BeautifulSoup(sources, "lxml")
    print(soup)

html_pin()

请告诉我我的代码有什么问题?我认为这是数据类型错误,但是当我尝试应用 type(sources) 函数时,我得到了响应 class 'str'

试试这个:

from bs4 import BeautifulSoup
from selenium import webdriver

def html_pin():
    browser = webdriver.Chrome()
    browser.get('http://FULL_URL')
    sources = browser.page_source
    browser.quit()
    soup = BeautifulSoup(sources, "lxml")
    print(soup)

html_pin()

您正在导入 bs4 模块,给它一个自定义的 BeautifulSoup 别名,然后尝试 call/instantiate 这个别名的 bs4 模块。

相反,您需要从 bs4 模块导入 BeautifulSoup class:

from bs4 import BeautifulSoup

请注意,现代 IDE 确实有助于避免此类问题,这是我将您的代码粘贴到编辑器时在 PyCharm 中看到的内容: