这篇文章主要讲解了“怎么用python代码实现爬取奥特曼图片”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“怎么用python代码实现爬取奥特曼图片”吧!

爬取网址:http://www.ultramanclub.com/allultraman/

使用工具:pycharm,requests

进入网页

打开开发者工具

点击 Network

刷新网页,获取信息

其中的Request URL就是我们所爬取的网址

滑到最下有一个User-Agent,复制

向服务器发送请求

200意味着请求成功

使用 response.text 获取文本数据

可以看到有些乱码

使用encode转换

importrequestsurl='http://www.ultramanclub.com/allultraman/'headers={'User-Agent':'Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/98.0.4758.82Safari/537.36'}response=requests.get(url=url,headers=headers)html=response.textHtml=html.encode('iso-8859-1').decode('gbk')print(Html)

接下来开始爬取需要的数据

使用Xpath获得网页链接

要使用Xpath必须先导入parsel包

importrequestsimportparseldefget_response(html_url):headers={'User-Agent':'Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/98.0.4758.82Safari/537.36'}response=requests.get(url=html_url,headers=headers)returnresponseurl='http://www.ultramanclub.com/allultraman/'response=get_response(url)html=response.text.encode('iso-8859-1').decode('gbk')selector=parsel.Selector(html)period_hrefs=selector.xpath('//div[@class="btn"]/a/@href')#获取三个时代的网页链接forperiod_hrefinperiod_hrefs:print(period_href.get())

可以看到网页链接不完整,我们手动给它添加上去period_href = 'http://www.ultramanclub.com/allultraman/' + period_href.get()

进入其中一个网页

跟之前的操作一样,用Xpath获取奥特曼的网页信息

forperiod_hrefinperiod_hrefs:period_href='http://www.ultramanclub.com/allultraman/'+period_href.get()#print(period_href)period_response=get_response(period_href).textperiod_html=parsel.Selector(period_response)lis=period_html.xpath('//div[@class="ultraheros-Contents_Generations"]/div/ul/li/a/@href')forliinlis:print(li.get())

运行后同样发现链接不完整

li = 'http://www.ultramanclub.com/allultraman/' + li.get().replace('./','')

拿到网址后继续套娃操作,就可以拿到图片数据

png_url='http://www.ultramanclub.com/allultraman/'+li_selector.xpath('//div[@class="left"]/figure/img/@src').get().replace('../','')

完整代码

importrequestsimportparselimportosdirname="奥特曼"ifnotos.path.exists(dirname):#判断是否存在名称为奥特曼的文件夹,没有就创建os.mkdir(dirname)defget_response(html_url):headers={'User-Agent':'Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/98.0.4758.82Safari/537.36'}response=requests.get(url=html_url,headers=headers)returnresponseurl='http://www.ultramanclub.com/allultraman/'response=get_response(url)html=response.text.encode('iso-8859-1').decode('gbk')selector=parsel.Selector(html)period_hrefs=selector.xpath('//div[@class="btn"]/a/@href')#获取三个时代的网页链接forperiod_hrefinperiod_hrefs:period_href='http://www.ultramanclub.com/allultraman/'+period_href.get()period_html=get_response(period_href).textperiod_selector=parsel.Selector(period_html)lis=period_selector.xpath('//div[@class="ultraheros-Contents_Generations"]/div/ul/li/a/@href')forliinlis:li='http://www.ultramanclub.com/allultraman/'+li.get().replace('./','')#获取每个奥特曼的网址#print(li)li_html=get_response(li).textli_selector=parsel.Selector(li_html)url=li_selector.xpath('//div[@class="left"]/figure/img/@src').get()#print(url)ifurl:png_url='http://www.ultramanclub.com/allultraman/'+url.replace('.','')png_title=li_selector.xpath('//ul[@class="lists"]/li[3]/text()').get()png_title=png_title.encode('iso-8859-1').decode('gbk')#print(li,png_title)png_content=get_response(png_url).contentwithopen(f'{dirname}\\{png_title}.png','wb')asf:f.write(png_content)print(png_title,'图片下载完成')else:continue

当爬到 奈克斯特奥特曼的时候,就会返回None,调了半天,也没搞懂,所以用if url:语句跳过了奈克斯特奥特曼,有没有大佬知道原因

url=li_selector.xpath('//div[@class="left"]/figure/img/@src').get()

感谢各位的阅读,以上就是“怎么用python代码实现爬取奥特曼图片”的内容了,经过本文的学习后,相信大家对怎么用python代码实现爬取奥特曼图片这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!