python多线程爬取-今日头条的街拍数据(附源码加思路注释)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python多线程爬取-今日头条的街拍数据(附源码加思路注释),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3165字,纯文字阅读大概需要5分钟。
内容图文
这里用的是json+re+requests+beautifulsoup+多线程
1 import json 2 import re 3 from multiprocessing.pool import Pool 4 5import requests 6from bs4 import BeautifulSoup 7from config import * 8from requests import RequestException 9 10 11def get_page_index(offset, keyword): 12‘‘‘得到一个页面的索引‘‘‘ 13 data = { 14‘offset‘: offset, 15‘format‘: ‘json‘, 16‘keyword‘: keyword, 17‘autoload‘: ‘true‘, 18‘count‘: ‘20‘, 19‘cur_tab‘: ‘1‘, 20‘from‘: ‘search_tab‘ 21 } 22# 请求方式一 23# url = ‘https://www.toutiao.com/search_content/?‘+urlencode(data) 24# response = requests.get(url) 25 26# 请求方式二 27 url = ‘https://www.toutiao.com/search_content/‘ 28try: 29 response = requests.get(url, params=data) 30if response.status_code == 200: 31return response.text 32return None 33except RequestException: 34return None 35 36 37def parse_page_index(html): 38‘‘‘解析json数据‘‘‘ 39 data = json.loads(html) 40if data and‘data‘in data.keys(): 41for item in data.get(‘data‘): 42yield item.get(‘article_url‘) 43 44 45def get_page_detail(url): 46‘‘‘得到详情页的数据‘‘‘ 47# 添加的请求头 48 headers = { 49‘user-agent‘: ‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36‘, 50 } 51try: 52 response = requests.get(url, headers=headers) 53if response.status_code == 200: 54return response.text 55return None 56except RequestException: 57return None 58 59 60def parse_page_detail(html, url): 61‘‘‘解析详情页数据‘‘‘ 62 soup = BeautifulSoup(html, ‘lxml‘) 63 t = soup.select(‘title‘) 64for i in t: 65 title = i.get_text() 66 67 pattern = re.compile(‘gallery: JSON.parse\("(.*?)"\),‘, re.S) 68 result = re.search(pattern, html) 69if result: 70 71# print(result.group(1)) 72 d = re.sub(‘\\\\‘, ‘‘, result.group(1)) 73# print(d) 74 data = json.loads(d) 75if data: 76 images = [item.get(‘url‘) for item in data.get(‘sub_images‘)] 77for image in images: 78 download_image(image, title) 79return { 80‘title‘: title, 81‘url‘: url, 82‘images‘: images 83 } 84else: 85 None 86 87 88def download_image(url, title): 89‘‘‘ 90 图片下载 91 :param url: 下载的连接 92 :return: 93‘‘‘ 94print(‘正在下载‘, url) 95try: 96 response = requests.get(url) 97if response.status_code == 200: 98 content = response.content 99 save_to_image(content, title) 100return None 101except RequestException: 102return None 103104105 count = 0 106107108def save_to_image(content, title): 109global count 110‘‘‘111 保存图片文件 112 :param content: 图片文件的内容 113 :return: 114‘‘‘115 name = title + str(count) 116 file_path = ‘./头条/{}.{}‘.format(name, ‘jpg‘) 117 with open(file_path, ‘wb‘) as f: 118 count += 1 119 f.write(content) 120121122def main(offset): 123‘‘‘主程序入口‘‘‘124 html = get_page_index(offset, ‘街拍‘) 125126# print(html)127for url in parse_page_index(html): 128129if url: 130# print(url)131 html = get_page_detail(url) 132if html: 133# print(parse_page_detail(html, url))134 result = parse_page_detail(html, url) 135if result: 136print(result) 137# save_to_mongo(result)138139140 GROUP_START = 1 141 GROUP_END = 20 142if__name__ == ‘__main__‘: 143 groups = [i * 20 for i in range(GROUP_START, GROUP_END)] 144 pool = Pool() 145 pool.map(main, groups)
原文:https://www.cnblogs.com/yunlongaimeng/p/9572148.html
内容总结
以上是互联网集市为您收集整理的python多线程爬取-今日头条的街拍数据(附源码加思路注释)全部内容,希望文章能够帮你解决python多线程爬取-今日头条的街拍数据(附源码加思路注释)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。