本文主要是介绍爬取需要的图片,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
爬取图片
import re,requests,time#导入所需要的库headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0","Cookie":"BDqhfp=%E5%8A%9E%E5%85%AC%E5%AE%A4%E7%9D%A1%E8%A7%89%26%26NaN-1undefined%26%260%26%261; winWH=%5E6_2000x1036; BDIMGISLOGIN=0; BDUSS=EpTSG13eEZldzFMOWJ5dWR4eDd3ZW5KUHdjNGNSN1lHNlJ4MXF6SU0wRTdwZUptRUFBQUFBJCQAAAAAAAAAAAEAAAADYpx-v8mwrrXEwfXesd6xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADsYu2Y7GLtmb; BDUSS_BFESS=EpTSG13eEZldzFMOWJ5dWR4eDd3ZW5KUHdjNGNSN1lHNlJ4MXF6SU0wRTdwZUptRUFBQUFBJCQAAAAAAAAAAAEAAAADYpx-v8mwrrXEwfXesd6xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADsYu2Y7GLtmb; BAIDUID=4D17154262B4504CFAE0E1C130A58739:FG=1; BIDUPSID=4D17154262B4504CFAE0E1C130A58739; PSTM=1723594384; H_WISE_SIDS=60450_60515_60566_60447_60574_60360_60622; BDSFRCVID=QgIOJeC62xZZPyJt1coJtHooIDYB-w6TH6_nkYIn5XtsdSiEoGHiEG0PRx8g0KFbwpqVogKKBmOTHn4F_2uxOjjg8UtVJeC6EG0Ptf8g0f5; H_BDCLCKID_SF=tb4t_CLbfI-3fP36q46Eh4-jMfQX--QXHD7yWCvl5hTcOR5Jj65WWlIr2t6JJ4JyWjn7Wb7Y3UTIKt0C3MA-BPCfWtrXtlvJ-KngBl5O3hnEsq0x0h5We-bQyPja-bODQDOMahvPal7xObumQlPK5JkgMx6MqpQJQeQ-5KQN3KJmfbL9bT3tjjISKx-_J6KqJbTP; H_PS_PSSID=60450_60515_60360_60622_60664_60677; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; BDSFRCVID_BFESS=QgIOJeC62xZZPyJt1coJtHooIDYB-w6TH6_nkYIn5XtsdSiEoGHiEG0PRx8g0KFbwpqVogKKBmOTHn4F_2uxOjjg8UtVJeC6EG0Ptf8g0f5; H_BDCLCKID_SF_BFESS=tb4t_CLbfI-3fP36q46Eh4-jMfQX--QXHD7yWCvl5hTcOR5Jj65WWlIr2t6JJ4JyWjn7Wb7Y3UTIKt0C3MA-BPCfWtrXtlvJ-KngBl5O3hnEsq0x0h5We-bQyPja-bODQDOMahvPal7xObumQlPK5JkgMx6MqpQJQeQ-5KQN3KJmfbL9bT3tjjISKx-_J6KqJbTP; BAIDUID_BFESS=4D17154262B4504CFAE0E1C130A58739:FG=1; BDRCVFR[dG2JNJb_ajR]=mk3SLVN4HKm; userFrom=www.baidu.com; BDRCVFR[-pGxjrCMryR]=mk3SLVN4HKm; BDRCVFR[tox4WRQ4-Km]=mk3SLVN4HKm; delPer=0; PSINO=1; indexPageSugList=%5B%22%E5%A6%82%E4%BD%95%E6%89%BE%E5%87%BA%E6%B5%8F%E8%A7%88%E5%99%A8%E7%9A%84User-Agent%E5%92%8Ccooki%22%2C%22%E6%8A%BD%E7%83%9F%22%5D; cleanHistoryStatus=0; ab_sr=1.0.1_NmU1ZWNmMzhkY2I4ZmJlMGMzN2FkMTJhYTNjNjE4MmYxNTk0YjQyOTY0OTMxNzE1ZmZhODBiNTY4OTA0NTAwODA4NWFjYzk1NjgyODBhN2IyNjdhZGI4YTZkZWNkODM1MWY5MzhlNWZlZGM0ZDNhNGIyNmJkZTA3YTczMDUwMDI3MGJhZjczMjk4YzFkNDU4MTI2NzRlZjNhNGI4MGQxYg==; BA_HECTOR=a48k0ka184a4ak85a08ha001b2rs8p1jcnofs1u; ZFY=7jp5CbqgnncF8g3rGloEzlb:B5phany8Uza:BjKYbl3Sk:C",
}detail_urls = [] # 存储图片地址
save_path = r'C:\Users\SHINOW\Desktop\pic' # 设置图片保存的文件夹路径 for i in range(1,400,20):#20页一张# url = 'http://image.baidu.com/search/flip?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=&st=-1&fm=result&fr=&sf=1&fmq=1592804203005_R&pv=&ic=&nc=1&z=&hd=&latest=©right=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&ctd=1592804203008%5E00_1328X727&sid=&word=%E5%BE%B7%E9%B2%81%E7%BA%B3&pn={}'.format(i)#请求的地址# url = 'https://image.baidu.com/search/flip?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=&st=-1&fm=result&fr=&sf=1&fmq=1724212493397_R&pv=&ic=&nc=1&z=&hd=&latest=©right=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&dyTabStr=&ie=utf-8&ctd=1724212493398%5E00_1217X604&sid=&word=%E6%8A%BD%E7%83%9F&pn={}'.format(i)#请求的地址url = 'https://image.baidu.com/search/flip?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=&st=-1&fm=result&fr=&sf=1&fmq=1592804203005_R&pv=&ic=&nc=1&z=&hd=&latest=©right=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&ctd=1592804203008%5E00_1328X727&sid=&word=%e7%9d%a1%e5%b2%97&pn={}'.format(i)#请求的地址response = requests.get(url,headers,timeout = (3,7))#设置请求超时时间3-7秒content = response.content.decode('utf-8')#使用utf-8进行解码detail_url = re.findall('"objURL":"(.*?)"',content,re.DOTALL)#re.DOTALL忽略格式#匹配objURL的内容,大部分为objURL或URLdetail_urls.append(detail_url)#将获取到的图片地址保存在之前定义的列表中response = requests.get(url,headers=headers)#请求网站content = response.content
b = 0#图片第几张
for page in detail_urls:for url in page:try:print('获取到{}张图片'.format(i))response = requests.get(url,headers = headers)content = response.contentif url[-3:] == 'jpg':with open('保存的地址{}.jpg'.format(b),'wb') as f:f.write(content)elif url[-4:] == 'jpeg':with open('保存的地址{}.jpeg'.format(b),'wb') as f:f.write(content)elif url[-3:] == 'png':with open('保存的地址{}.pon'.format(b),'wb') as f:f.write(content)else:continueexcept:print('超时')b +=1
这篇关于爬取需要的图片的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!