日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python 斗图图片爬虫

發布時間:2023/12/20 python 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 斗图图片爬虫 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?搗鼓了三小時,有一些小Bug,望大佬指導

廢話不說,直接上代碼:

#!/usr/bin/python3 # -*- coding:UTF-8 -*- import os,re,requests from urllib import request,parseclass Doutu_api(object):def __init__(self):self.api_html = r'http://www.doutula.com/search?keyword=%s'self.headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 ''(KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36'}self.path = os.path.dirname(os.path.realpath(__file__))+'\\temp'def make_path(self,path=''):#返回假為已創建,否則創建新文件夾self.path = self.path+'\\'+pathif os.path.exists(self.path): # 判斷文件夾是否存在return Falseelse:os.mkdir(self.path) # 創建文件夾return Truedef get_img_html(self,html):self.make_path(path=html)html = self.api_html%parse.quote(html)pattern = re.compile(u'<a.*?class="col-xs-6 col-md-2".*?href="(.*?)".*?style="padding:5px;">.*?</a>',re.S)pattern_img = re.compile(u'<td>.*?<img.*?src="(.*?)".*?alt="(.*?)".*?οnerrοr=".*?">.*?</td>',re.S)try:req = request.Request(html, headers=self.headers)imgs = request.urlopen(req)imgs = imgs.read().decode('utf-8')imgs = re.findall(pattern, imgs)for img in imgs:req = request.Request(img, headers=self.headers)imgurl = request.urlopen(req).read().decode('utf-8')imgurl =re.findall(pattern_img, imgurl)with open(self.path+'\\{}.png'.format(imgurl[0][1].replace('/','-')), 'wb') as file:response = requests.get(imgurl[0][0]).content # 下載圖片file.write(response) # 讀取圖片print('已完成下載,圖片地址:',self.path)except Exception as e:print(e)return Nonedoutu = Doutu_api() doutu.get_img_html(input('斗圖內容關鍵字:'))

?測試成功

轉載于:https://www.cnblogs.com/canmeng/p/9180046.html

總結

以上是生活随笔為你收集整理的python 斗图图片爬虫的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。