0


使用Selenium来爬取网页内容

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

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

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

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

首先是要做好准备工作:

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

  1. 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。

第三步,开始爬取数据

案例代码如下:

  1. from selenium import webdriver
  2. from selenium.webdriver.common.by import By
  3. def getData_name():
  4. driver = webdriver.Chrome(r'E:\pythonProject1\chromedriver.exe') #浏览器驱动
  5. driver.get('https://www.ccgp-chongqing.gov.cn/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') #网址
  6. driver.implicitly_wait(10) #停留十秒
  7. with open('D:/house.txt', 'a+') as f:
  8. #for i in range(1,7): #手动设置爬取6条数据
  9. try:
  10. # if i > 2 :
  11. each1 = driver.find_elements(By.XPATH,"//div/h4/span[@style='font-size:18px;margin-right:20px;']") #相应属性的xpath路径
  12. # else:
  13. # each1 = driver.find_elements(By.XPATH,"//*[@id='index']/div/div[1]/div[3]/div["+str(i)+"]/div/div[2]/p") #相应属性的xpath路径
  14. # print(i)
  15. print(each1[0])
  16. f.write(each1[0].text + "\n")
  17. except:
  18. print("第{0}条数据处理失败".format(1))
  19. if __name__ == '__main__':
  20. getData_name()

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

定位方式如下:

1、按照ID定位

  1. <html>
  2. <body>
  3. <form id="loginForm">
  4. <input name="username" type="text" />
  5. <input name="password" type="password" />
  6. <input name="continue" type="submit" value="Login" />
  7. </form>
  8. </body>
  9. </html>

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

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

2、按名称定位

  1. <html>
  2. <body>
  3. <form id="loginForm">
  4. <input name="username" type="text" />
  5. <input name="password" type="password" />
  6. <input name="continue" type="submit" value="Login" />
  7. <input name="continue" type="button" value="Clear" />
  8. </form>
  9. </body>
  10. </html>

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

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

3、通过 XPath 定位

  1. <html>
  2. <body>
  3. <form id="loginForm">
  4. <input name="username" type="text" />
  5. <input name="password" type="password" />
  6. <input name="continue" type="submit" value="Login" />
  7. <input name="continue" type="button" value="Clear" />
  8. </form>
  9. </body>
  10. </html>

表单元素可以这样定位:

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

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

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

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

XPath Tutorial 学习

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

  1. <html>
  2. <body>
  3. <p>Are you sure you want to do this?</p>
  4. <a href="continue.html">Continue</a>
  5. <a href="cancel.html">Cancel</a>
  6. </body>
  7. </html>

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

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

5、按标签名称定位元素

  1. <html>
  2. <body>
  3. <h1>Welcome</h1>
  4. <p>Site content goes here.</p>
  5. </body>
  6. </html>

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

  1. heading1 = driver.find_element(By.TAG_NAME, 'p')
标签: selenium python 爬虫

本文转载自: https://blog.csdn.net/CoCo629vanilla/article/details/126266287
版权归原作者 Popuessing's Jersey 所有, 如有侵权,请联系我们删除。

“使用Selenium来爬取网页内容”的评论:

还没有评论