irpas技术客

使用Selenium来爬取网页内容_Popuessing's Jersey_selenium获取网页内容

大大的周 7541

简单介绍一下Selenium,以下是官方文档的解释:

Selenium Python 绑定提供了一个简单的 API 来使用 Selenium WebDriver 编写功能/验收测试。通过 Selenium Python API,我们可以直观地访问 Selenium WebDriver 的所有功能。

简单来说,Selenium就是python下面的一个工具包,他能够通过API调用Selenium WebDriver的功能。

那么如何通过Selenium来爬取数据呢?

首先是要做好准备工作:

第一步,下载Selenium。可以直接在pycharm中下载,也可以从终端下载使用pip安装 selenium 包。

pip install selenium

第二步,下载驱动。Selenium 需要驱动程序来与所选浏览器交互。例如,Firefox 需要geckodriver,Google需要googleDriver,需要先安装它们。

一些更流行的浏览器驱动程序的链接如下:

Google:https://sites.google.com/chromium.org/driver/ MicrosoftEdge:https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/ Firefox:https://github.com/mozilla/geckodriver/releases

下载好之后需要配置一下环境,将它放在/usr/bin或/usr/local/bin中。

否则会报错误?selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH。

第三步,开始爬取数据

案例代码如下:

from selenium import webdriver from selenium.webdriver.common.by import By def getData_name(): driver = webdriver.Chrome(r'E:\pythonProject1\chromedriver.exe') #浏览器驱动 driver.get('https://·/info-notice/procument-notice-detail/1111264751247470592?title=%E9%87%8D%E5%BA%86%E5%B7%A5%E7%A8%8B%E8%81%8C%E4%B8%9A%E6%8A%80%E6%9C%AF%E5%AD%A6%E9%99%A2%E6%97%A0%E4%BA%BA%E6%9C%BA%E5%A4%9A%E5%85%83%E6%95%B0%E6%8D%AE%E9%87%87%E9%9B%86%E4%B8%8E%E5%A4%84%E7%90%86%E7%B3%BB%E7%BB%9F%E8%AE%BE%E5%A4%87(CQS22A00383)%E4%B8%AD%E6%A0%87%EF%BC%88%E6%88%90%E4%BA%A4%EF%BC%89%E7%BB%93%E6%9E%9C%E5%85%AC%E5%91%8A') #网址 driver.implicitly_wait(10) #停留十秒 with open('D:/house.txt', 'a+') as f: #for i in range(1,7): #手动设置爬取6条数据 try: # if i > 2 : each1 = driver.find_elements(By.XPATH,"//div/h4/span[@style='font-size:18px;margin-right:20px;']") #相应属性的xpath路径 # else: # each1 = driver.find_elements(By.XPATH,"//*[@id='index']/div/div[1]/div[3]/div["+str(i)+"]/div/div[2]/p") #相应属性的xpath路径 # print(i) print(each1[0]) f.write(each1[0].text + "\n") except: print("第{0}条数据处理失败".format(1)) if __name__ == '__main__': getData_name()

我们需要在页面定位到需要爬取的元素的位置,然后进行爬取。

定位方式如下:

1、按照ID定位

<html> <body> <form id="loginForm"> <input name="username" type="text" /> <input name="password" type="password" /> <input name="continue" type="submit" value="Login" /> </form> </body> </html>

需要定位到表单元素,通过

login_form = driver.find_element(By.ID, 'loginForm')

2、按名称定位

<html> <body> <form id="loginForm"> <input name="username" type="text" /> <input name="password" type="password" /> <input name="continue" type="submit" value="Login" /> <input name="continue" type="button" value="Clear" /> </form> </body> </html>

用户名和密码元素可以这样定位:

username = driver.find_element(By.NAME, 'username') password = driver.find_element(By.NAME, 'password')

3、通过 XPath 定位

<html> <body> <form id="loginForm"> <input name="username" type="text" /> <input name="password" type="password" /> <input name="continue" type="submit" value="Login" /> <input name="continue" type="button" value="Clear" /> </form> </body> </html>

表单元素可以这样定位:

login_form = driver.find_element(By.XPATH, "/html/body/form[1]") 或者 login_form = driver.find_element(By.XPATH, "//form[1]") 或者 login_form = driver.find_element(By.XPATH, "//form[@id='loginForm']")

用户名元素可以像这样定位:

username = driver.find_element(By.XPATH, "//form[input/@name='username']") 或者 username = driver.find_element(By.XPATH, "//form[@id='loginForm']/input[1]") 或者 username = driver.find_element(By.XPATH, "//input[@name='username']")

XPath的定位比较复杂多样,这里涵盖不全,若果感兴趣可以去

XPath Tutorial?学习

4、通过链接文本定位超链接

<html> <body> <p>Are you sure you want to do this?</p> <a href="continue.html">Continue</a> <a href="cancel.html">Cancel</a> </body> </html>

continue.html 链接可以这样定位:

continue_link = driver.find_element(By.LINK_TEXT, 'Continue') continue_link = driver.find_element(By.PARTIAL_LINK_TEXT, 'Conti')

5、按标签名称定位元素

<html> <body> <h1>Welcome</h1> <p>Site content goes here.</p> </body> </html>

标题 (p) 元素可以这样定位:

heading1 = driver.find_element(By.TAG_NAME, 'p')


1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,会注明原创字样,如未注明都非原创,如有侵权请联系删除!;3.作者投稿可能会经我们编辑修改或补充;4.本站不提供任何储存功能只提供收集或者投稿人的网盘链接。

标签: #selenium获取网页内容 #selenium #Python #绑定提供了一个简单的 #API #来使用 #webdriver