Python WebDriver如何打印整个页面源代码(html)
我正在将Python2.7与SeleniumWebDriver一起使用。 我的问题是如何使用Python WebDriver如何打印整个页面源代码(html),python,selenium-webdriver,webdriver,Python,Selenium Webdriver,Webdriver,我正在将Python2.7与SeleniumWebDriver一起使用。 我的问题是如何使用print方法打印整页源代码。 有webdriver方法page\u source,但它返回webdriver,我不知道如何将其转换为字符串,或者只在webdriver实例上的终端中打印它: >>> from selenium import webdriver >>> driver = webdriver.Firefox() >>> driver.ge
print
方法打印整页源代码。
有webdriver方法page\u source
,但它返回webdriver,我不知道如何将其转换为字符串,或者只在webdriver
实例上的终端中打印它:
>>> from selenium import webdriver
>>> driver = webdriver.Firefox()
>>> driver.get('http://google.com')
>>> print(driver.page_source)
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" itemtype="http://schema.org/WebPage" itemscope=""><head><meta name="descri
...
:before,.vscl.vslru div.vspib{top:-4px}</style></body></html>
来自selenium导入webdriver的>
>>>driver=webdriver.Firefox()
>>>司机,上车http://google.com')
>>>打印(驱动程序页\源)
您也可以在不使用浏览器的情况下获取HTML页面源代码。请求模块允许您这样做
import requests
res = requests.get('https://google.com')
res.raise_for_status() # this line trows an exception if an error on the
# connection to the page occurs.
print(res.text)
谢谢,这正是我需要的!这是我的错,因为我这样做的方式很糟糕。打印驱动程序。page\u source
(driver.page\u source不在括号中)page\u source似乎只是可见的,而不是整个html源代码。