爬虫学习案例3

打印 上一主题 下一主题

主题 913|帖子 913|积分 2739

爬取玉人图片

精美图库地点
一页图片
安装依靠库文件

  1. pip install selenium requests beautifulsoup4
  2. import time
  3. import requests
  4. import random
  5. from bs4 import BeautifulSoup
  6. from selenium import webdriver
  7. from selenium.webdriver.chrome.service import Service
  8. from selenium.webdriver.chrome.options import Options
  9. # 设置Chrome选项
  10. chrome_options = Options()
  11. chrome_options.add_argument("--headless")  # 无头模式,不打开浏览器窗口
  12. chrome_options.add_argument("--disable-gpu")
  13. chrome_options.add_argument("--no-sandbox")
  14. # 设置ChromeDriver路径
  15. service = Service('D:\env\python3\chromedriver.exe')
  16. url = 'https://www.umei.cc/touxiangtupian/nvshengtouxiang/'
  17. baseUrl = "https://www.umei.cc"
  18. # 初始化WebDriver
  19. driver = webdriver.Chrome(service=service, options=chrome_options)
  20. driver.get(url)
  21. time.sleep(random.uniform(5, 10))  # 等待页面加载
  22. html = driver.page_source # 原页面
  23. soup = BeautifulSoup(html, 'html.parser')
  24. # print(soup)
  25. # BeautifulSoup分析提取元素
  26. divList = soup.find_all("div",class_= "item masonry_brick")
  27. # print(divList)
  28. # 一个美女信息
  29. for divItem in divList:
  30.     linkImage = divItem.find("div",class_ = "item_t").find("div",class_ = "img").find("a")["href"]
  31.     linkImage = baseUrl + linkImage
  32.     # 拿去子页面的大图
  33.     driver.get(linkImage)
  34.     time.sleep(random.uniform(5, 10))
  35.     html = driver.page_source
  36.     sonSoup = BeautifulSoup(html, 'html.parser')
  37.     imgUrl = sonSoup.find("div",class_ = "tsmaincont-main-cont-txt").find("img")["src"]
  38.     print(f"准备下载图片{imgUrl}")
  39.     # 下载图片
  40.     img_response = requests.get(imgUrl)
  41.     img_name = imgUrl.split('/')[-1]
  42.     with open("img"+img_name, "wb") as f:
  43.         f.write(img_response.content)
  44.     print(f"图片{img_name}下载完成")
  45. print("第一页图片全部下载到当前目录了.....")
  46. driver.quit()  # 关闭浏览器
复制代码

爬取多页

  1. import time
  2. import requests
  3. import random
  4. from bs4 import BeautifulSoup
  5. from selenium import webdriver
  6. from selenium.webdriver.chrome.service import Service
  7. from selenium.webdriver.chrome.options import Options
  8. # 设置Chrome选项
  9. chrome_options = Options()
  10. chrome_options.add_argument("--headless")  # 无头模式,不打开浏览器窗口
  11. chrome_options.add_argument("--disable-gpu")
  12. chrome_options.add_argument("--no-sandbox")
  13. # 设置ChromeDriver路径
  14. service = Service('D:\env\python3\chromedriver.exe')
  15. url = 'https://www.umei.cc/touxiangtupian/nvshengtouxiang/'
  16. baseUrl = "https://www.umei.cc"
  17. # 初始化WebDriver
  18. driver = webdriver.Chrome(service=service, options=chrome_options)
  19. def getImage(url,page):
  20.     driver.get(url)
  21.     print(f"正在爬取第{page}页图片资源源...")
  22.     print(url)
  23.     time.sleep(random.uniform(5, 10))  # 等待页面加载
  24.     html = driver.page_source # 原页面
  25.     soup = BeautifulSoup(html, 'html.parser')
  26.     # BeautifulSoup分析提取元素
  27.     divList = soup.find_all("div",class_= "item masonry_brick")
  28.     for divItem in divList:
  29.         linkImage = divItem.find("div",class_ = "item_t").find("div",class_ = "img").find("a")["href"]
  30.         linkImage = baseUrl + linkImage
  31.         # 拿取子页面的大图
  32.         driver.get(linkImage)
  33.         time.sleep(random.uniform(5, 10))
  34.         html = driver.page_source
  35.         sonSoup = BeautifulSoup(html, 'html.parser')
  36.         imgUrl = sonSoup.find("div",class_ = "tsmaincont-main-cont-txt").find("img")["src"]
  37.         print(f"准备下载图片{imgUrl}")
  38.         # 下载图片
  39.         img_response = requests.get(imgUrl)
  40.         img_name = imgUrl.split('/')[-1]
  41.         with open("img"+img_name, "wb") as f:
  42.             f.write(img_response.content)
  43.         print(f"图片{img_name}下载完成")
  44.     print(f"第{page}页图片全部下载到当前img目录了.....")
  45. # 爬取1-10页
  46. # 控制爬取的页面数
  47. for page in range(1, 11):
  48.     if page == 1:
  49.         getImage(url,page)
  50.     else:
  51.         pageUrl = f"{url}index_{page}.htm"
  52.         getImage(pageUrl,page)
  53. driver.quit()  # 关闭浏览器
复制代码



免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

西河刘卡车医

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表