python爬蟲-妹子圖


python爬蟲之妹子圖


懂的人都懂!

import urllib.request
import os
import re
import time

#關於re模塊使用的連接https://www.cnblogs.com/shenjianping/p/11647473.html

def url_open(url):
    #header = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.18362"}
    header={"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36 QIHU 360SE"}
    req = urllib.request.Request(url,headers=header)
    response = urllib.request.urlopen(req)
    html = response.read()
    return html

def find_resource(url):
    html = url_open(url).decode("utf-8") #獲取網頁
    #print(html) ##查看網頁
    img_addrs = re.findall('''<img alt=".*?" src="(.*?)" />''',html) ##改動處
    #print(img_addrs) ##查看地址
    return img_addrs

def save_imgs(floder,img_addrs):
    for each in img_addrs:
        time.sleep(0.3)
        filename = each.split('/')[-1]
        with open(filename,'ab') as f:
            if re.findall("^\\bh",each) == []:
                each = "https://www.xiuaa.com" + each #當圖片地址不完整時使用
            img = url_open(each) #打開圖片地址
            f.write(img) #下載圖片

def main(floder='download'): #主程序,傳入文件夾名稱參數
    os.chdir(floder)
    
    for i in range(0,10): #翻頁
        num = i
        url = "https://www.xiuaa.com/xgmn/4492_" +str(num) + ".html" ##改動處
        img_addrs = find_resource(url)
        save_imgs(floder,img_addrs)
        print("第",i+1,"張..")
    
    print("爬取完畢!")

if __name__ == '__main__':
    main()

2020.8.12嘗試了可以運行


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM