Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.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 Selenium:如何将整个html作为字符串获取?_Python_Selenium - Fatal编程技术网

Python Selenium:如何将整个html作为字符串获取?

Python Selenium:如何将整个html作为字符串获取?,python,selenium,Python,Selenium,我将Selenium与python一起使用。请参阅以下代码: from selenium.webdriver.common.keys import Keys import selenium.webdriver driver = selenium.webdriver.Firefox() driver.get("http://finance.yahoo.com/q?s=APP") 现在,我想做一件简单的事情:从驱动程序获取该web的html作为字符串。然后,我可以使用beautifulsou来解析

我将Selenium与python一起使用。请参阅以下代码:

from selenium.webdriver.common.keys import Keys
import selenium.webdriver
driver = selenium.webdriver.Firefox()
driver.get("http://finance.yahoo.com/q?s=APP")
现在,我想做一件简单的事情:从
驱动程序
获取该web的html作为字符串。然后,我可以使用
beautifulsou
来解析它。有人知道吗

实际上,我不知道如何从这个
驱动程序中获取信息,例如,在这种情况下获取苹果的股价

我对硒是完全陌生的。一个很好的教程,它是高度赞赏

谢谢大家!

你要找的

要继续您的示例,请执行以下操作:

soup = BeautifulSoup(driver.page_source)
然而,正如另一位评论者所指出的,您可以使用类似的库来达到相同的效果:

r = requests.get('http://finance.yahoo.com/q?s=APP')
soup = BeautifulSoup(r.content)

看看下面的代码

from selenium.webdriver.common.keys import Keys
import selenium.webdriver
driver = selenium.webdriver.Firefox()
driver.get("http://finance.yahoo.com/q?s=APP")
page_html = driver.page_source

在html页面中,您将看到打开页面的html。

您使用Selenium进行此操作的具体原因是什么?如果您只想提取html以用于
BeautifulSoup
,那么这几乎肯定是过火了,您应该使用类似
requests
的方法。原因是要刮取的web是通过无限滚动实现的。我想使用驱动程序一次又一次地向下滚动到web底部,以逐渐爬网该web中的所有数据。我知道这在计算上效率很低。但我现在没有更好的解决办法。你有什么更好的建议吗?原因是我想抓取的网站是用无限卷轴实现的。我想使用驱动程序一次又一次地向下滚动到底部,以便刮取所有数据。这在计算上是否过于昂贵,甚至不可能?如果是这样的话,你有什么好的建议来处理这个无限卷轴吗?