【Python爬虫】爬虫示例---爬取必应美图壁纸(正则表达式)

【Python爬虫】爬虫示例---爬取必应美图壁纸(正则表达式),第1张

【Python爬虫】爬虫示例---爬取必应美图壁纸(正则表达式)

我是新手小白,其中有许多不足之处望大家谅解。如有更好的方法,希望可以交流交流。

引入time是因为,程序里面有多次requests get请求,再加上循环,如果下载量大,服务器和本机IP会受到影响。前两天爬取58同城时,访问次数太多,IP被禁了。

话不多说,直接来看源代码。 

import requests
import re
import time
home_url='https://bing.ioliu.cn/?p='
add_str='https://bing.ioliu.cn/'
headers={
 "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36 Edg/96.0.1054.62"
}
obj=re.compile(r'(?P.*?)©.*?(?P.*?).*?.*?)" target=',re.S)
print("当前日期是:",time.strftime('%Y.%m.%d',time.localtime(time.time())))
print("该网站每页显示12张图片,并且每天更新","n最早的是2016.03.05","n请输入下载区间(以回车符区别):")
for n in range(int(int(input())),int(input())+1):
 url1=home_url+str(n)
 print("当前是第%d页"%n,"n网站链接为%s"%url1)
 resp = requests.get(url1,headers=headers).text
 res=obj.finditer(resp)
 for i in res:
  per_link=add_str+i.group('link')
  name_dates=i.group('date')+'@'+i.group('name').replace(' (','')
  jpg_address=requests.get(per_link,headers=headers)
  with open(name_dates+'.jpg',mode='wb') as f:
   f.write(jpg_address.content)
   print('GET--->',name_dates)
   time.sleep(1)
print('OVER!')

 运行示例:

 

如果有纰漏之处,望指出,大家共同学习,共同进步。 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5690068.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存