Python WebDriver how to print whole page source (html)

后端 未结 2 830
-上瘾入骨i
-上瘾入骨i 2020-12-03 14:06

I\'m using Python 2.7 with Selenium WebDriver. My question is how to print whole page source with print method. There is webdriver method page_source

相关标签:
2条回答
  • 2020-12-03 14:27

    .page_source on a webdriver instance is what you need:

    >>> 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>
    
    0 讨论(0)
  • 2020-12-03 14:29

    You can also get the HTML page source without using a browser. The requests module allows you to do that.

     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)
    
    0 讨论(0)
提交回复
热议问题