ʯäÁÔª ·¢±íÓÚ 2020-8-31 11:58

pythonͼƬץȡԴÂë

import requests
from lxml import etree


# ¶¨Òåαװͷ£¬ÅûÉÏÂí¼×¼Ù×°×Ô¼ºÊÇä¯ÀÀÆ÷
header = {
    # refererÊÇÓÃÀ´»ñÈ¡HTTPµÄÀ´Ô´µØÖ·£¬¼òµ¥Ëµ¾ÍÊÇrefererÐÅÏ¢»á¸æËß·þÎñÆ÷Ë­·¢ÆðµÄÇëÇó¡£
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36",
    "referer": "http://www.huitu.com/topic-detail/3857.html?recType=0"
}

# 1¡¢ÇëÇóÃÃ×ÓͼÄõ½ÍøÕ¾ÕûÌåÊý¾Ý
response = requests.get("http://www.huitu.com/topic-detail/3857.html?recType=0", headers=header)

# 2¡¢Êý¾Ý½âÎöÔ¤´¦Àí
html = etree.HTML(response.text)

# 3¡¢»ñÈ¡±êÌâÃû×Ö
alt_list = html.xpath('//img[@class="lazy"]/@alt')
# print(src_list)

# 4¡¢»ñÈ¡ÏÂÔØÁ´½Ó
src_list = html.xpath('//img[@class="lazy"]/@data-original')
# print(src_list)

# ±éÀúÁ½¸öÁбí,ÐèÒªÌáÇ°½¨Á¢photoÎļþ¼Ð
for alt, src in zip(alt_list, src_list):
    # 5¡¢Í¨¹ýÏÂÔØÁ´½ÓÇëÇóͼƬÊý¾Ý
    image = requests.get(url=src, headers=header)

    # 6¡¢±£´æͼƬ
    file_name = "./photo/" + alt + ".jpg"
    print("ÕýÔÚ±£´æͼƬÎļþ£º" + file_name)
    with open(file_name, "wb") as file:
      file.write(image.content)
ÒÔÉÏ´úÂë½ö¹©Ñ§Ï°Ê¹ÓÃ,ϲ»¶µÄÇ×ÃǶà¶àµãÔÞ¹ÄÀøÏÂÀϵܣ¬ÎÒÊÇÐÂÈË¡£

C¸ç888 ·¢±íÓÚ 2020-8-31 14:15

×¢ÊͺÜÏêϸ£¬²»´í

ʯäÁÔª ·¢±íÓÚ 2020-9-1 09:45

лл´ó¼Ò£¬ÎÒ»¹»áŬÁ¦µÄ
Ò³: [1]
²é¿´ÍêÕû°æ±¾: pythonͼƬץȡԴÂë