大自然的搬运工——爬取美女图片
本帖最后由 wp19910107 于 2021-4-9 16:26 编辑初学python,其实也不算初学了,就是说白了,白嫖而已。{:1_905:}
看着论坛这么多大神写的代码,反正都下载下来试了试,运行失败,(由于技术不行完全看不懂,所以以失败告终),但是有一天希望之光他来了,在看到一个大神写的代码的时候,真的重新燃起斗志了{:1_899:}(唯一不足是小图,但是这又有何妨呢,白嫖的有图就行了。要什么自行车)。
下面附上大神写的代码和大神原页面,这么好的东西不能让他消失殆尽。需要学习的可以去大神原页面学习交流;【笔记】python自学笔记(爬虫篇)——妹子图实战
https://www.52pojie.cn/thread-1379875-1-1.html
(出处: 吾爱破解论坛){:1_893:}
import requests
from bs4 import BeautifulSoup
import random #导入random包
import os
import threading
def ua_list(): #定义一个随机UA的子程序
USER = [ #定义一个UA列表,UA可以在百度搜索到
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; AcooBrowser; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Acoo Browser; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506)",
"Mozilla/4.0 (compatible; MSIE 7.0; AOL 9.5; AOLBuild 4337.35; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",
"Mozilla/5.0 (Windows; U; MSIE 9.0; Windows NT 9.0; en-US)",
"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 2.0.50727; Media Center PC 6.0)",
"Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 1.0.3705; .NET CLR 1.1.4322)",
"Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.0.04506.30)",
"Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN) AppleWebKit/523.15 (KHTML, like Gecko, Safari/419.3) Arora/0.3 (Change: 287 c9dfb30)",
"Mozilla/5.0 (X11; U; Linux; en-US) AppleWebKit/527+ (KHTML, like Gecko, Safari/419.3) Arora/0.6",
"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.2pre) Gecko/20070215 K-Ninja/2.1.1",
"Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9) Gecko/20080705 Firefox/3.0 Kapiko/3.0",
"Mozilla/5.0 (X11; Linux i686; U;) Gecko/20070322 Kazehakase/0.4.5",
"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.8) Gecko Fedora/1.9.0.8-1.fc10 Kazehakase/0.5.6",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.20 (KHTML, like Gecko) Chrome/19.0.1036.7 Safari/535.20",
"Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; fr) Presto/2.9.168 Version/11.52",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.11 TaoBrowser/2.0 Safari/536.11",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.71 Safari/537.1 LBBROWSER",
"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; LBBROWSER)",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 732; .NET4.0C; .NET4.0E; LBBROWSER)",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.84 Safari/535.11 LBBROWSER",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)",
"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; QQBrowser/7.0.3698.400)",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 732; .NET4.0C; .NET4.0E)",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; SV1; QQDownload 732; .NET4.0C; .NET4.0E; 360SE)",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 732; .NET4.0C; .NET4.0E)",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)",
"Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1",
"Mozilla/5.0 (iPad; U; CPU OS 4_2_1 like Mac OS X; zh-cn) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8C148 Safari/6533.18.5",
"Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:2.0b13pre) Gecko/20110307 Firefox/4.0b13pre",
"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:16.0) Gecko/20100101 Firefox/16.0",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11",
"Mozilla/5.0 (X11; U; Linux x86_64; zh-CN; rv:1.9.2.10) Gecko/20100922 Ubuntu/10.10 (maverick) Firefox/3.6.10"
]
headers = {'User-Agent': random.choice(USER)} #choice是random包的一个方法,是从列表中随机取一个值
return headers #return是将headers返回到调用这个子程序的地方
def get_html(page,semaphore): #爬取写在这个子程序中,传入的这个page参数是在多线程中传过来的
semaphore.acquire()
url = f'https://www.mzitu.com/page/{page}/'
headers = ua_list() #从ua_list子程序中随机选择个UA传入
html = requests.get(url = url,headers = headers) #访问这个网站
#print(html.text)
soup = BeautifulSoup(html.text,'lxml') #bs4解析
results = soup.select('ul#pins li a img')
for result in results:
print(result['data-original'])
title = result['alt']
img = result['data-original']
with open(f'meizitu/{title}.jpg','wb') as filedata:
print("正在下载图片")
filedata.write(requests.get(img).content)
print(f"第{page}页图片下载完成")
semaphore.release()
if __name__ == "__main__":
if not os.path.exists("meizitu"): #判断一下这个目录下有没有meizitu这个文件夹
os.mkdir("meizitu") #没有的话就创建,到时候图片存储在这个文件夹下
semaphore = threading.BoundedSemaphore(5) #最多同时允许五个线程同时运行
for i in range(20): #这里你可以改for i in range(数值,数值)
t = threading.Thread(target=get_html,args =(i,semaphore))
t.start()
while threading.active_count()!=1:
pass
else:
print("全部图片下载完毕")
真的是小白,连代码怎么发都搞不明白,只能拿出自己独特的方式了,(其实大家都会:粘贴复制{:1_937:}),自己试了试,还是可以的。下面附上运行图片:
。
好了不多说了,大家有缘再会了,(主要是文采不行,剩下心里面只能说了:感谢大神,大神牛逼了。)我不会写代码,但是我是大自然的搬运工。 你爬的那个网站,顺带手说说吧,白票到底了 来看这个的都是狼!狼友们好! 绅士们好 真不会用,有真货吗?{:1_918:} 学习学习,感谢分享 学习了,感谢分享哈哈:lol 代码有用,很好。谢谢 chenpenglei 发表于 2021-4-9 09:30
你爬的那个网站,顺带手说说吧,白票到底了
代码url里面写的有,
mengyang 发表于 2021-4-9 09:33
真不会用,有真货吗?
看大哥的实力还是可以的呀,咋不会勇,谦虚了。 我也是小白,你给的这一大串,我就算知道粘贴复制,我粘贴到哪里去用呀,就像你给我一个工具不给我说明书一样。