|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- from urllib.parse import urlencode
- import requests
- import os
- from multiprocessing import Pool
- import time
- base_url = 'https://www.toutiao.com/api/search/content/?'
- headers = {
- 'Referer':'https://www.toutiao.com/search/?keyword=%E5%9B%BE%E7%89%87',
- 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36',
- 'X-Requested-With':'XMLHttpRequest'
- }
- def get_page(offset):
- params = {
- 'aid': 24,
- 'app_name': 'web_search',
- 'offset': offset,
- 'format': 'json',
- 'keyword': '街拍',
- 'count': 20,
- 'en_qc': 1,
- 'cur_tab': 1,
- 'from': 'search_tab',
- 'pd': 'synthesis',
- 'timestamp': '1584520318379'
- }
- url = base_url + urlencode(params)
- try:
- response = requests.get(url, headers=headers)
- response.raise_for_status()
- response.encoding = response.apparent_encoding
- return response.json()
- except requests.ConnectionError as e:
- print('Error', e.args)
- def get_images(json):
- if json.get('data'):
- for item in json.get('data'):
- title = item.get('title')
- images = item.get('image_list')
- for image in images:
- yield {
- 'image':image.get('url'),
- 'title':title
- }
- def save_image(item):
- if not os.path.exists(item.get('title')):
- os.mkdir(item.get('title'))
- try:
- response = requests.get(item.get('image'))
- if response.status_code == 200:
- file_path = '{0}/{1}.{2}'.format(item.get('title'), time.time(), 'jpg')
- if not os.path.exists(file_path):
- with open(file_path, 'wb', encoding='utf8') as f:
- f.write(response.content)
- else:
- print('已经存在', file_path)
- except requests.ConnectionError:
- print('下载不成功')
- def main(offset):
- json = get_page(offset)
- for item in get_images(json):
- print(item)
- save_image(item)
- GROUP_START = 1
- GROUP_END = 10
- if __name__ == '__main__':
- pool = Pool()
- groups = ([x * 20 for x in range(GROUP_START, GROUP_END + 1)])
- pool.map(main, groups)
- pool.close()
复制代码
麻烦请教大神,我在爬头条图片时,代码成功完成了,但是并没有输出任何东西,值显示 Process finished with exit code 0
网上查了后还是解决不了,是python解释器设置问题么?还是其他
。。。
二进制写入数据还有encoding???
- from urllib.parse import urlencode
- import requests
- import os
- from multiprocessing import Pool
- import time
- base_url = 'https://www.toutiao.com/api/search/content/?'
- headers = {
- 'Referer':'https://www.toutiao.com/search/?keyword=%E5%9B%BE%E7%89%87',
- 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36',
- 'X-Requested-With':'XMLHttpRequest'
- }
- def get_page(offset):
- params = {
- 'aid': 24,
- 'app_name': 'web_search',
- 'offset': offset,
- 'format': 'json',
- 'keyword': '街拍',
- 'count': 20,
- 'en_qc': 1,
- 'cur_tab': 1,
- 'from': 'search_tab',
- 'pd': 'synthesis',
- 'timestamp': '1584520318379'
- }
- url = base_url + urlencode(params)
- try:
- response = requests.get(url, headers=headers)
- response.raise_for_status()
- response.encoding = response.apparent_encoding
- return response.json()
- except requests.ConnectionError as e:
- print('Error', e.args)
- def get_images(json):
- if json.get('data'):
- for item in json.get('data'):
- title = item.get('title')
- images = item.get('image_list')
- for image in images:
- yield {
- 'image':image.get('url'),
- 'title':title
- }
- def save_image(item):
- if not os.path.exists(item.get('title')):
- os.mkdir(item.get('title'))
- try:
- response = requests.get(item.get('image'))
- if response.status_code == 200:
- file_path = '{0}/{1}.{2}'.format(item.get('title'), time.time(), 'jpg')
- if not os.path.exists(file_path):
- with open(file_path, 'wb') as f:
- f.write(response.content)
- else:
- print('已经存在', file_path)
- except requests.ConnectionError:
- print('下载不成功')
- def main(offset):
- json = get_page(offset)
- for item in get_images(json):
- print(item)
- save_image(item)
- GROUP_START = 1
- GROUP_END = 10
- if __name__ == '__main__':
- pool = Pool()
- groups = ([x * 20 for x in range(GROUP_START, GROUP_END + 1)])
- pool.map(main, groups)
- pool.close()
复制代码
|
|