Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.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
Python WebDriver如何打印整个页面源代码(html)_Python_Selenium Webdriver_Webdriver - Fatal编程技术网

Python WebDriver如何打印整个页面源代码(html)

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

我正在将Python2.7与SeleniumWebDriver一起使用。 我的问题是如何使用
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源代码。