您正试图通过网址打开页面。 open()
不会那么做的,使用urlopen()
:
from urllib.request import urlopen # Python 3
# from urllib2 import urlopen # Python 2
url = "your target url here"
soup = bs4.BeautifulSoup(urlopen(url), "html.parser")
或者使用对人类的HTTP - requests
library:
import requests
response = requests.get(url)
soup = bs4.BeautifulSoup(response.content, "html.parser")
还要注意,强烈建议specify a parser explicitly - 我在这个使用html.parser
情况下,还有其他解析器可用。
I want to use the exact same page(same instance)
一种常见的方式做到这一点是让driver.page_source
并将其传递给BeautifulSoup
进一步解析:
from bs4 import BeautifulSoup
from selenium import webdriver
driver = webdriver.Firefox()
driver.get(url)
# wait for page to load..
source = driver.page_source
driver.quit() # remove this line to leave the browser open
soup = BeautifulSoup(source, "html.parser")
参见[如何让整个页面的innerHTML的硒驱动程序( https://stackoverflow.com/questions/35905517/how-to-get-innerhtml-of-whole-page-in-selenium-driver) – robyschek