python爬虫三国杀十周年

查看 177|回复 10
作者:zxc693122   
[Python] 纯文本查看 复制代码import urllib.request
from lxml import etree
import json
# https://www.sanguosha.com/msgs/mWallPaper
# https://www.sanguosha.com/msgs/mWallPaper/cur/2
# https://www.sanguosha.com/msgs/mWallPaper/cur/3
def get_content(page):
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
                      "Chrome/115.0.0.0 Safari/537.36"}
    if page == 1:
        first_url = "https://www.sanguosha.com/msgs/mWallPaper"
        request_first = urllib.request.Request(url=first_url, headers=headers)
        response = urllib.request.urlopen(request_first)
        content_first = response.read().decode("utf-8")
        num = 0
        tree = etree.HTML(content_first)
        https_list = tree.xpath("/html/body/div[1]/div/div//@href")
        for i in range(len(https_list)):
            url = https_list
            num += 1
            urllib.request.urlretrieve(url=url, filename="D:/三国杀十周年壁纸/" + "00" + str(num) + ".jpg")
    else:
        url = "https://www.sanguosha.com/msgs/mWallPaper/cur/" + str(page)
        request = urllib.request.Request(url=url, headers=headers)
        response = urllib.request.urlopen(request)
        content = response.read().decode("utf-8")
        with open("三国杀十周年壁纸爬虫.json", "w", encoding="utf-8") as file:
            file.write(content)
def json_data():
    json_file = json.load(open("三国杀十周年壁纸爬虫.json", "r", encoding="utf-8"))
    for i in range(len(json_file)):
        https_data = json_file["imgurl"]
        name = json_file["title"].replace("*", "-")
        urllib.request.urlretrieve(url=https_data, filename="D:/三国杀十周年壁纸/" + name + ".jpg")
if __name__ == '__main__':
    begin_page = int(input("请输入起始页码:"))
    end_page = int(input("请输入起始结束:"))
    for page in range(begin_page, end_page + 1):
        get_content(page)
        json_data()

十周年, 爬虫

gujun0416   

推荐使用下边这种方式插入代码
【公告】发帖代码插入以及添加链接教程(有福利)
https://www.52pojie.cn/thread-713042-1-1.html
(出处: 吾爱破解论坛)
jrwapj   

不错,感谢楼主分享,共同进步!
rosewood   

感谢楼主分享,支持一波
余律师   

不错,感谢楼主分享,共同进步!
xiaopeng128   

谢谢您的分享!
加奈绘   

谢谢分享
1225661221   

支持楼主,谢谢分享
两个人的地球   

牛蛙牛蛙
t1r0   

谢谢楼主分享
您需要登录后才可以回帖 登录 | 立即注册