菜鸡源码,专注精品下载!
当前位置:首页 > 建站教程 > 建站知识

Python代码实现百度图库批量爬取下载

发布时间:2024-01-05  栏目:建站知识   浏览:   分类:python教程 Python爬取

要使用Python批量爬取并下载百度图库的图片,可以使用`requests`库和`BeautifulSoup`库。首先,安装这两个库: ```bash pip install requests pip install beautifulsoup4 ``` 然后,编写代码: ```python import os import requests from bs4 import BeautifulSoup def download_image(url, save_path): response = requests.get(url) with open(save_path, 'wb') as f: f.write(response.content) def batch_download_images(keyword, num_images): url = f'https://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&is=&fp=result&queryWord={keyword}&

#@风清扬(fqy2022)importrequestsimporttimeimportos#创建保存文件夹ifos.path.isdir(r'./保存'):print('已存在文件夹!')else:os.mkdir('./保存')print('已为您创建文件夹!')classImage(object):def__init__(self):#URLself.url='https://image.baidu.com/search/acjson?'#拼接headersself.headers={'Cookie':'BDqhfp=%E7%8B%97%26%260-10-1undefined%26%260%26%261;BIDUPSID=A063B6D6CC13957DA917CAA433A26251;PSTM=1583301079;MCITY=-315%3A;BDUSS=TBSSlRRQU9QbmR-MGt6NUFQa01iR3VQWHBUbnNacW9zMnJUN0N-QndGSzNkMkJnSVFBQUFBJCQAAAAAAAAAAAEAAADuVM9dw~vX1tPQybbIobXEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAALfqOGC36jhgS;BDUSS_BFESS=TBSSlRRQU9QbmR-MGt6NUFQa01iR3VQWHBUbnNacW9zMnJUN0N-QndGSzNkMkJnSVFBQUFBJCQAAAAAAAAAAAEAAADuVM9dw~vX1tPQybbIobXEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAALfqOGC36jhgS;BAIDUID=857FDC525D72D7899014BED3AB7A9EFF:FG=1;__yjs_duid=1_bd666ba46de51678e9fb98774eb68df71616750528301;BDORZ=FFFB88E999055A3F8A630C64834BD6D0;BDSFRCVID_BFESS=0X0OJeCmHlQJPareecEsuUw4D2KK0gOTHllnm4-TLeKNvakVJeC6EG0Ptf8g0KubFTPRogKK0gOTH6KF_2uxOjjg8UtVJeC6EG0Ptf8g0M5;H_BDCLCKID_SF_BFESS=fRkfoKPKfCv8qTrmbtOhq4tHePPLexRZ5mAqoJIXQCjvDR5eD4TD3J-0jhbhtPvLtnTnaIQhtqQnqnQTXPoYBpku5bOR2f743bRT2MKy5KJvfj6gjj7qhP-UyPkHWh37aGOlMKoaMp78jR093JO4y4Ldj4oxJpOJ5JbMonLafD_bhD-4Djt2eP00-xQja--XKKj2WROeajrjDnCrDhA2XUI8LUc72poZLI6H0R5J34OhSt0mQ55vyT8sXnO72P7XaRPL-pRHWhr-HJvKy4oTjxL1Db3JKjvMtg3t3qQmLUooepvoD-Jc3MvByPjdJJQOBKQB0KnGbUQkeq8CQft20b0EeMtjW6LEK5r2SCDMtC0b3D;indexPageSugList=%5B%22%E7%8B%97%22%2C%22%E4%BA%8C%E5%93%88%22%2C%22%E9%87%87%E8%80%B3%E5%9B%BE%E7%89%87%20%E5%94%AF%E7%BE%8E%22%2C%22%E9%87%87%E8%80%B3%E5%9B%BE%E7%89%87%E9%AB%98%E6%B8%85%22%2C%22%E9%87%87%E8%80%B3%E5%AE%A3%E4%BC%A0%E5%9B%BE%E7%89%87%22%2C%22%E9%87%87%E8%80%B3%22%2C%22%E5%96%9D%E5%80%92%E4%BA%86%E7%9A%84%E8%A1%A8%E6%83%85%E5%8C%85%22%2C%22%E8%A5%BF%E6%B8%B8%E8%AE%B0%20%E8%AF%8D%E4%BA%91%22%2C%22%E5%AD%99%E6%82%9F%E7%A9%BA%20%E8%AF%8D%E4%BA%91%22%5D;delPer=0;PSINO=7;BDRCVFR[dG2JNJb_ajR]=mk3SLVN4HKm;BDRCVFR[-pGxjrCMryR]=mk3SLVN4HKm;BDRCVFR[EJrvrN3l0S0]=pDgu-4B3j7tIZ-EIy7GQhPEUf;H_PS_PSSID=;BDRCVFR[X_XKQks0S63]=mk3SLVN4HKm;firstShowTip=1;ZD_ENTRY=baidu;cleanHistoryStatus=0;BA_HECTOR=a401010ka584240lm51g6r0320r;userFrom=www.baidu.com;ab_sr=1.0.0_YjAxODJmMjA1MDU3YTUyZjIyMzk2MGQ4YjM3MTQ5OGNjNDI5NWFkNjkxOTA0YjkxMDBlYjY0Y2JmMDU5NzY5MDY1NDAxZDY0ZDhhYjUzZDhkNGY4ZDUwOWVhMzkwMGMxYzQ5OTA1MjE3OTViYzZmN2QxNzMyN2M2ZjYxMzBkYTE=','User-Agent':'Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/89.0.4389.114Safari/537.36'}self.params={'tn':'resultjson_com','logid':'11625870838566749778','ipn':'rj','ct':'201326592','is':'','fp':'result','queryWord':'','cl':'2','lm':'-1','ie':'utf-8','oe':'utf-8','adpicid':'','st':'-1','z':'','ic':'0','hd':'','latest':'','copyright':'','word':'','s':'','se':'','tab':'','width':'','height':'','face':'0','istype':'2','qc':'','nc':'1','fr':'','expermode':'','force':'','pn':'','rn':'30','gsm':'','time':''}self.image_list=[]a=input('请输入要爬取的图片名称:')self.params['queryWord']=aself.params['word']=adefget_image(self,num):foriinrange(0,num):self.params['time']=int(time.time()*1000)self.params['pn']=i*30response=requests.get(url=self.url,headers=self.headers,params=self.params)forjinrange(0,len(response.json()['data'])-1):self.image_list.append(response.json()['data'][j]['thumbURL'])#图片保存函数defsave_image(self):n=1foriinself.image_list:image=requests.get(url=i)print('正在下载第{}张'.format(n))withopen('./保存/{}.jpg'.format(n),'wb')asf:f.write(image.content)n+=1if__name__=='__main__':c=int(input('请输入要爬取的页数(每页有30张图片):'))image=Image()image.get_image(c)image.save_image()


相关文章

    无相关信息
评论
建站知识
建站知识
使用技巧
调试安装
运营推广