使用BeatifulSoup错误进行Python解析
当我运行这段代码时,我得到一个错误 汤=美汤(来源,“lxml”) TypeError:“模块”对象不可调用使用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(
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导入系统,非常感谢,克里斯!