实战爬取某网站图片-Python

直接上代码

#!/usr/bin/python
# -*- coding: UTF-8 -*-
from bs4 import BeautifulSoup
import requests
import sys
import os
 
#获取图集id
try:
    headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36 Edg/92.0.902.55"}
    html = requests.get('https://www.tujigu.com/', headers=headers)
    html.encoding = 'utf-8-sig'
    soup = BeautifulSoup(html.text,"html.parser").find_all('p',class_='biaoti')
    dict = {}
    for i in soup:
        id_url = i.find('a')['href']
        id = id_url[25:len(id_url)-1]
        name_str = i.find('a').contents[0]
        name = name_str.replace(" ", "")
        dict.update({id:name})
except:
    print("获取id和name失败")
    sys.exit()
 
 
for id,name in dict.items():
    #创建目录
    path= r"C:\Users\ATRAY\Documents\Downloads\{}".format(name)
    folder = os.path.exists(path)
    if not folder:                   
        os.makedirs(path)            
    else:
        print ("创建目录失败")
    #下载图片
    num = 1
    while True:
        picture_url="https://tjg.gzhuibei.com/a/1/{}/{}.jpg".format(id,num)
        picture=requests.get(picture_url,headers=headers)
        if picture.status_code == 200:
            with open(path + "\{}.jpg".format(num),"wb") as code:
                code.write(picture.content)
            num = num + 1
        else:
            print("以下载{}写真{}张".format(name,num - 1))
            break
暂无评论

发送评论 编辑评论


				
上一篇
下一篇