如何有效管理和优化你的网页插件? | 网页插件管理
856
2022-10-02
bing背景图--python3的grequests测试
1 # !usr/bin/python3.4 2 # -*- coding:utf-8 -*- 3 4 import json 5 import grequests 6 import requests 7 import re 8 import time 9 10 def geturl(urls):11 12 sn = requests.Session()13 rs = [grequests.get(url, session=sn) for url in urls]14 15 return grequests.map(rs)16 17 def get(url):18 19 header = {'User-Agent':20 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0',21 'Referer':' 'Host': 'cn.bing.com'}23 24 # 解析网页25 html_bytes = requests.get(url, headers=header)26 return html_bytes27 28 # 去除标题中的非法字符 (Windows)29 def validateTitle(title):30 # '/\:*?"<>|'31 rstr = r"[\/\\:\*\?\"\<\>\|]"32 new_title = re.sub(rstr, "", title)33 return new_title34 35 if __name__ == '__main__':36 i = 037 img = []38 imgname = []39 while True:40 url = '+ str(i) + '&n=1'41 42 contents =get(url)43 data = contents.content.decode('utf-8', 'ignore')44 data = json.loads(data)45 try:46 onefile = data['images']47 for item in onefile:48 img.append(item['url'])49 imgname.append(item['copyright'].replace(' ', ''))50 print(img[i])51 i = i + 152 except Exception as err:53 print(err)54 break55 56 print('已经搜集好网址...')57 print('暂停3秒后开始批量-图片,请保持网络畅通...')58 time.sleep(3)59 print('正在-...')60 pics = geturl(img)61 62 j = 063 for pic in pics:64 filenamep = '../jpg/' + validateTitle(imgname[j] + '.jpg')65 filess = open(filenamep, 'wb')66 filess.write(pic.content)67 filess.close()68 print('已经写入第' + str(j + 1) + '张图片')69 j = j + 1
发现bing搜索的背景图非常好看:
所以写了个代码把他全部-下来总的来说grequests非常好用
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~