鱼C论坛

 找回密码
 立即注册
查看: 3357|回复: 10

[作品展示] 昨天又去爬了下,我学习爬虫后,爬的第一个网站,知道是那个吗?

[复制链接]
发表于 2019-6-1 23:05:09 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

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

x

不要宣扬,放过头来看当初爬它的时候, 各种百度,各种难题,哈哈,回过头来,这种静态的已经是so easy 了,还能变着花样爬它。
  1. from lxml import etree
  2. from OpenSSL import SSL
  3. import requests
  4. import re
  5. import time
  6. import os

  7. HEADERS = {
  8.     'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36",
  9.     'Referer': 'http://www.mzitu.com'
  10. }
  11. QUANTITY =220#图集页数  每页/24

  12. FILE_PATH = None #下载路径,请设置绝对路径,默认以当前绝对路径做保存



  13. def get_path(name,num=" "):
  14.     '''
  15.     当num为None时,用来查询目录是否存在
  16.     :param name:
  17.     :param num:
  18.     :return:
  19.     '''
  20.     currrent_path = os.path.realpath(__file__)  # 文件绝对路径
  21.     current_dir = os.path.split(currrent_path)[0]
  22.     file_path = os.path.join(current_dir, name)
  23.     if num==" ":
  24.         if not os.path.exists(file_path):
  25.             return False
  26.     if FILE_PATH is None:
  27.         if not os.path.exists(file_path):
  28.             os.makedirs(file_path)
  29.     else:
  30.         file_path = FILE_PATH
  31.     return os.path.join(file_path,num) + ".jpg"

  32. def get_response(url):
  33.     """返回URL响应"""
  34.     time.sleep(2)
  35.     return requests.get(url=url,headers=HEADERS)

  36. def atlas():
  37.     '''
  38.     :yield:图集下载的地址
  39.     '''
  40.     for i in range(QUANTITY):
  41.         url = "https://www.mzitu.com/page/{page}/".format(page=i)
  42.         response = get_response(url=url).text
  43.         imgurl_list = re.findall(r'<li><a href="(.*?)" target="_blank">',response)
  44.         imgname_list = re.findall(r"alt='(.*?)' width=",response)
  45.         for img_naem,img_url in zip(imgname_list,imgurl_list):
  46.             item = {}
  47.             item["name"] = img_naem
  48.             print(img_naem)
  49.             # if not get_path(name=img_naem):
  50.             #     break
  51.             item["img_url"] = img_url
  52.             yield item


  53. def get_download_url(item):
  54.     """
  55.     :param url:图片url
  56.     :return:  图集下载地址
  57.     """
  58.     response = get_response(url=item["img_url"]).text
  59.     etre = etree.HTML(response)
  60.     num = etre.xpath("//div[@class='pagenavi']/a[5]/span/text()")
  61.     for i in range(0,int(num[0])+1):
  62.         item["download"] = item["img_url"]+ "/" + str(i)
  63.         item["number"] = str(i)
  64.         yield item


  65. def download(item):
  66.     headers = {
  67.         'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36",
  68.         'Referer': item["download"]
  69.     }
  70.     try:
  71.         time.sleep(2)
  72.         etre = etree.HTML(requests.get(url=item["download"],headers=headers).text)
  73.         download_url = etre.xpath("//div[@class='main-image']/p/a/img/@src")[0]
  74.         response = requests.get(url=download_url,headers=headers).content
  75.         file_path = get_path(item["name"],item["number"])
  76.         with open(file_path,"wb") as fp:
  77.             fp.write(response)
  78.     except SSL.SysCallError as e:
  79.         print("当前出现错误%s"%e)
  80.         print(item)



  81. def product(c):
  82.     c.send(None)
  83.     for img in atlas():
  84.         c.send(img)
  85.     c.close()


  86. def customer():
  87.     data = ""
  88.     while True:
  89.         n = yield data
  90.         if not n:
  91.             return
  92.         for each in get_download_url(item=n):
  93.             download(each)

  94. def main():
  95.     func = customer()
  96.     product(func)


  97. if __name__ == '__main__':
  98.     main()
复制代码

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-6-1 23:54:12 | 显示全部楼层
Traceback (most recent call last):
  File "C:/Users/Administrator/AppData/Local/Programs/Python/Python37-32/Save/1.py", line 1, in <module>
    from lxml import etree
ModuleNotFoundError: No module named 'lxml'
一脸蒙蔽
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-6-2 00:58:43 | 显示全部楼层
青梅 发表于 2019-6-1 23:54
Traceback (most recent call last):
  File "C:/Users/Administrator/AppData/Local/Programs/Python/Pyt ...

缺少lxml库,安装这个库,pip install lxml  
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2019-6-2 23:31:08 | 显示全部楼层
Stubborn 发表于 2019-6-2 00:58
缺少lxml库,安装这个库,pip install lxml

哦  晓得了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-11 07:35:56 | 显示全部楼层
看不懂,懵了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-7-25 11:13:24 | 显示全部楼层
大佬进步神速
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-9-14 21:44:48 | 显示全部楼层
进步神速,厉害
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-11-6 19:05:43 | 显示全部楼层
Traceback (most recent call last):
  File "C:/Users/14028/Desktop/爱词霸.py", line 2, in <module>
    from OpenSSL import SSL
ModuleNotFoundError: No module named 'OpenSSL
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-11-6 19:23:04 | 显示全部楼层
解技 发表于 2019-11-6 19:05
Traceback (most recent call last):
  File "C:/Users/14028/Desktop/爱词霸.py", line 2, in
    from ...

pip install OpenSSl  少一个库
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-11-6 20:26:12 | 显示全部楼层
Stubborn 发表于 2019-11-6 19:23
pip install OpenSSl  少一个库

ERROR: Could not find a version that satisfies the requirement OpenSSL (from versions: none)
ERROR: No matching distribution found for OpenSSL
不知道为什么安装不了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-11-6 20:44:15 | 显示全部楼层
解技 发表于 2019-11-6 20:26
ERROR: Could not find a version that satisfies the requirement OpenSSL (from versions: none)
ERRO ...

那就不要了,在89行,异常捕捉,直接用except:   不指定具体错误。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-26 08:24

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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