博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python爬虫:python获取各种街拍美图
阅读量:2381 次
发布时间:2019-05-10

本文共 2122 字,大约阅读时间需要 7 分钟。

  1. 抓包

Python爬虫:python获取各种街拍美图

2. 查看参数信息

多看几页即可看见规律,主要改变的项无非是offset,timestamp,这里的stamp是13位的时间戳,再根据keyword改变搜索项,可以改变offset值实现翻页操作,其他的都是固定项

Python爬虫:python获取各种街拍美图
3. 数据解析

返回的数据中可以得到具体的栏目,image_list中是所有的图片链接,我们解析这个栏目,然后根据title下载图片即可

Python爬虫:python获取各种街拍美图
4. 流程分析

构建url发起请求,改变offset的值执行便利操作,对返回的json数据进行解析,根据title名称建立文件夹,如果栏目含有图片,则以title_num的格式下载图片

import requestsimport osimport timeheaders = { 'authority': 'www.toutiao.com', 'method': 'GET', 'path': '/api/search/content/?aid=24&app_name=web_search&offset=100&format=json&keyword=%E8%A1%97%E6%8B%8D&autoload=true&count=20&en_qc=1&cur_tab=1&from=search_tab&pd=synthesis&timestamp=1556892118295', 'scheme': 'https', 'accept': 'application/json, text/javascript', 'accept-encoding': 'gzip, deflate, br', 'accept-language': 'zh-CN,zh;q=0.9', 'content-type': 'application/x-www-form-urlencoded', 'referer': 'https://www.toutiao.com/search/?keyword=%E8%A1%97%E6%8B%8D', 'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36', 'x-requested-with': 'XMLHttpRequest',}def get_html(url): return requests.get(url, headers=headers).json()def get_values_in_dict(list): result = [] for data in list: result.append(data['url']) return resultdef parse_data(url): text = get_html(url) for data in text['data']: if 'image_list' in data.keys(): title = data['title'].replace('|', '') img_list = get_values_in_dict(data['image_list']) else: continue if not os.path.exists('街拍/' + title): os.makedirs('街拍/' + title) for index, pic in enumerate(img_list): with open('街拍/{}/{}.jpg'.format(title, index + 1), 'wb') as f: f.write(requests.get(pic).content) print("Download {} Successful".format(title))def get_num(num): if isinstance(num, int) and num % 20 == 0: return num else: return 0def main(num): for i in range(0, get_num(num) + 1, 20): url = 'https://www.toutiao.com/api/search/content/?aid={}&app_name={}&offset={}&format={}&keyword={}&' \ 'autoload={}&count={}&en_qc={}&cur_tab={}&from={}&pd={}&timestamp={}'.format(24, 'web_search', i, 'json', '街拍', 'true', 20, 1, 1, 'search_tab', 'synthesis', str(time.time())[:14].replace('.', '')) parse_data(url)if __name__ == '__main__': main(40)

转载地址:http://pazab.baihongyu.com/

你可能感兴趣的文章
Emoji表情符号录入MySQL数据库报错的解决方案
查看>>
Linux系统CentOS6.2版本下安装JDK7详细过程
查看>>
Android Studio之Activity切换动画(三)
查看>>
我是怎样和Linux系统结缘并通过红帽RHCE认证的
查看>>
DIYer最担心的事来了!CPU降价彻底无望
查看>>
WannaCry勒索软件还在继续传播和感染中
查看>>
为发展中国家儿童提供的OLPC OS 13.2.10 发布
查看>>
帅的代价!无框车门冻死:特斯拉一招解决
查看>>
美银美林提高Intel科技股的股票评级
查看>>
专家预测2019年的网络安全形势
查看>>
简单聊聊Linux学习经历
查看>>
欧盟即将在免费开源软件项目中推行“漏洞赏金”
查看>>
苹果股价下跌会迎来iPhone最黑暗时刻吗?
查看>>
智能校服受到多数学生追捧
查看>>
这么多CPU/显卡成就是AMD首创:大写的YES
查看>>
java实现解压缩(Unzip)功能的实现
查看>>
java操作Access *.mdb数据库的实现
查看>>
jdbc连接数据库的代码片段
查看>>
X86汇编:debug命令详解
查看>>
flex(通过URLLoader)与后台jsp进行交互的例子,包括中文乱码的处理
查看>>