你难道不想知道怎么用Python爬取性感美女壁纸?

百度性感美女壁纸了解一下

看到这个图片,有没有一种.........emmmmm.......刺激、兴奋的感觉

不管你们有没有 反正小编我是有一股冲劲的,自从知道了Python爬虫之后,只要看到有妹子的照片的网站,我就是想要批量下载一下!

不为别的,是为了能更好的学习Python! 我这样说你们信吗?

准备:

Python3.6

import requests

import json

完整代码

 1 # !/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 
 4 import requests
 5 import json
 6 
 7 
 8 # 定义一个请求函数,接收页面参数
 9 def get_page(page):
10     # 把页面参数添加在url的字符串当中
11     url = 'https://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&is=&fp=result&queryWord=美女&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=-1&z=&ic=0&word=美女&s=&se=&tab=&width=&height=&face=0&istype=2&qc=&nc=1&fr=&cg=girl&pn={}&rn=30&gsm=1e'.format(
12         page)
13     # 请求网站,并且得到网站的响应
14     #Python学习交流群:125240963,群内每天分享干货,包括最新的python企业案例学习资料和零基础入门教程,欢迎各位小伙伴入群学习交流
15     response = requests.get(url)
16     # 判断状态的状况
17     if response.status_code == 200:
18         # 返回文本文件信息
19         return response.text
20 
21 
22 def json_load(text):
23     # 把文本文件处理成字典格式
24     jsondict = json.loads(text)
25     # 创建一个空的合集,作用是去重
26     urlset = set()
27     # 检查字典里面是否包含了data这个值
28     if 'data' in jsondict.keys():
29         # 从jsondict中取出data这个字典里面的东西,依次赋值给items!
30         for items in jsondict.get('data'):
31             # 异常处理,不是每一行数据都包含thumbURL这个数据的
32             try:
33                 urlset.add(items['thumbURL'])
34             except:
35                 pass
36     return urlset
37 
38 
39 def down_cont(url):
40     response = requests.get(url)
41     name = url.split(',')[-1].split('&')[0]
42     if response.status_code == 200:
43         # 表示,如果文件名字相同,就删除当前文件,然后再创建一个一样名字的文件
44         with open('./images/%s.jpg' % name, 'wb') as f:
45             print('正在下载当前图片: ' + url)
46             # 以二进制的方法写入到本地
47             f.write(response.content)
48 
49 
50 def main():
51     for p in range(5):
52         print('正在下载 %s页 的图片' % p)
53         page = p * 30
54         text = get_page(page)
55         urlset = json_load(text)
56         for url in urlset:
57             down_cont(url)
58 
59 
60 if __name__ == '__main__':
61     main()

运行结果

猜你喜欢

转载自www.cnblogs.com/Python6359/p/9156538.html