Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用BeatifulSoup错误进行Python解析_Python_Selenium_Parsing_Beautifulsoup_Lxml - Fatal编程技术网

使用BeatifulSoup错误进行Python解析

使用BeatifulSoup错误进行Python解析,python,selenium,parsing,beautifulsoup,lxml,Python,Selenium,Parsing,Beautifulsoup,Lxml,当我运行这段代码时,我得到一个错误 汤=美汤(来源,“lxml”) TypeError:“模块”对象不可调用 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(

当我运行这段代码时,我得到一个错误 汤=美汤(来源,“lxml”) TypeError:“模块”对象不可调用

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()
请告诉我代码中有什么问题?我认为这是数据类型错误,但当我尝试应用类型(源)函数时,我得到了响应类'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
别名,然后尝试调用/实例化这个别名为
bs4
的模块

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

from bs4 import BeautifulSoup

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


来自bs4导入系统,非常感谢,克里斯!