用python批量下载国内某网站的图片
在这里用到了requests来发送请求 ,json,re,bs4来对数据进行解析,下面是源代码示例
import requests
import json
import re
from tqdm import tqdm
from bs4 import BeautifulSoup
def Spaider(num, page):
url = 'https://webapi.8btc.com/bbt_api/news/list?num={}&page={}&cat_id=242'.format(num, page)
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'
}
response = requests.get(url, headers=headers).content
json_str = re.findall(r'\[.+\]', str(response.decode('utf-8')))
datas = json.loads(str(json_str[0]))
json_data = json.dumps(datas)
print(json_data)
with open('csv_json.json', 'w',encoding='utf-8') as f:
f.write(json_data)
print(len(datas))
for i in tqdm(datas, '正在下载图片'):
image_url = i['image']
image_name = image_url.split('/')[-1]
name = './image/' + image_name
image_response = requests.get(image_url, stream=True)
with open(name, 'wb') as f:
for chunk in image_response.iter_content(chunk_size=128):
f.write(chunk)
for i in tqdm(datas, '正在下载图片'):
images_url = i['images'][1]
images_name = images_url.split('/')[-1]
name = './image/' + images_name
images_response = requests.get(images_url, stream=True)
with open(name, 'wb') as f:
for chunk in images_response.iter_content(chunk_size=128):
f.write(chunk)
if __name__ == '__main__':
for i in range(1, 100):
Spaider(50, i)