如何在 python 中使用 Selenium 和 Beautifulsoup 解析网站? [英] How can I parse a website using Selenium and Beautifulsoup in python?
本文介绍了如何在 python 中使用 Selenium 和 Beautifulsoup 解析网站?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
编程新手,并想出了如何使用 Selenium 导航到我需要去的地方.我想现在解析数据,但不知道从哪里开始.有人能握住我的手一秒钟,并为我指明正确的方向吗?
New to programming and figured out how to navigate to where I need to go using Selenium. I'd like to parse the data now but not sure where to start. Can someone hold my hand a sec and point me in the right direction?
感谢任何帮助-
推荐答案
假设您在要解析的页面上,Selenium 将源 HTML 存储在驱动程序的 page_source
属性中.然后将 page_source
加载到 BeautifulSoup
中,如下所示:
Assuming you are on the page you want to parse, Selenium stores the source HTML in the driver's page_source
attribute. You would then load the page_source
into BeautifulSoup
as follows:
In [8]: from bs4 import BeautifulSoup
In [9]: from selenium import webdriver
In [10]: driver = webdriver.Firefox()
In [11]: driver.get('http://news.ycombinator.com')
In [12]: html = driver.page_source
In [13]: soup = BeautifulSoup(html)
In [14]: for tag in soup.find_all('title'):
....: print tag.text
....:
....:
Hacker News
这篇关于如何在 python 中使用 Selenium 和 Beautifulsoup 解析网站?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文