当前位置:   article > 正文

python使用webdriver来进行自动爬取数据_python webdriver 获取circle数据

python webdriver 获取circle数据
  1. from selenium import webdriver
  2. import time
  3. import os
  4. import uuid
  5. import requests
  6. rows = []
  7. def recursion_spider():
  8. # 搜索金立手机
  9. input.send_keys("金立手机")
  10. searchBtn = chrome.find_element_by_css_selector("button.button > i")
  11. # 搜索按钮的点击
  12. searchBtn.click()
  13. # 等待页面加载完成
  14. time.sleep(5)
  15. chrome.execute_script("window.scrollTo(0,document.body.scrollHeight)")
  16. phoneLis = chrome.find_elements_by_class_name("gl-item")
  17. for phone in phoneLis:
  18. sku = phone.get_attribute("data-sku")
  19. title = phone.find_element_by_css_selector("div.p-img>a").get_attribute("title")
  20. imgUrl = phone.find_element_by_css_selector("div.p-img>a>img").get_attribute("src")
  21. if imgUrl is None:
  22. imgSrc = phone.find_element_by_css_selector("div.p-img>a>img").get_attribute(
  23. "data-lazy-img")
  24. else:
  25. imgSrc = phone.find_element_by_css_selector("div.p-img>a>img").get_attribute("src")
  26. if not imgSrc.startswith("https"):
  27. imgSrc = "https:" + imgSrc
  28. price = phone.find_element_by_css_selector("div.p-price>strong").text
  29. rows.append({"title": title, "imgSrc": imgSrc, "price": price})
  30. print(price)
  31. dirPath = "D:/images"
  32. if not os.path.exists(dirPath):
  33. os.mkdir(dirPath)
  34. print(imgSrc)
  35. data = requests.get(imgSrc)
  36. file = open(dirPath + "/" + str(uuid.uuid1()) + ".png", "wb")
  37. file.write(data.content)
  38. file.close()
  39. next_page_button = chrome.find_element_by_class_name("pn-next")
  40. if next_page_button is not None:
  41. next_page_button.click()
  42. recursion_spider()
  43. chrome = webdriver.Chrome()
  44. chrome.get("https://www.jd.com/")
  45. input = chrome.find_element_by_id("key")
  46. recursion_spider()

递归翻页查询,都是模拟chrome来进行数据的爬取,可谓是大大的方便!!!

  1. chrome = webdriver.Chrome()
  2. chrome.get("https://www.jd.com/")

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/364490
推荐阅读
相关标签
  

闽ICP备14008679号