温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么用python代码实现爬取奥特曼图片

发布时间:2022-02-12 18:55:34 来源:亿速云 阅读:396 作者:iii 栏目:开发技术

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

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

使用工具:pycharm,requests

进入网页

怎么用python代码实现爬取奥特曼图片

打开开发者工具

怎么用python代码实现爬取奥特曼图片

点击 Network

怎么用python代码实现爬取奥特曼图片

 刷新网页,获取信息

怎么用python代码实现爬取奥特曼图片

怎么用python代码实现爬取奥特曼图片

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

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

怎么用python代码实现爬取奥特曼图片

 向服务器发送请求

怎么用python代码实现爬取奥特曼图片

200意味着请求成功

使用 response.text 获取文本数据

怎么用python代码实现爬取奥特曼图片

 可以看到有些乱码

使用encode转换

import requests
 
url = 'http://www.ultramanclub.com/allultraman/'
 
headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36'
}
 
response = requests.get(url = url,headers=headers)
html = response.text
Html=html.encode('iso-8859-1').decode('gbk')
print(Html)

怎么用python代码实现爬取奥特曼图片

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

使用Xpath获得网页链接

要使用Xpath必须先导入parsel包

import requests
import parsel
 
def get_response(html_url):
    headers = {
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36'
    }
 
    response = requests.get(url = html_url,headers=headers)
    return response
 
url = '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')  #获取三个时代的网页链接
 
for period_href in period_hrefs:
    print(period_href.get())

怎么用python代码实现爬取奥特曼图片

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

怎么用python代码实现爬取奥特曼图片

 进入其中一个网页

怎么用python代码实现爬取奥特曼图片

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

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

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

怎么用python代码实现爬取奥特曼图片

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

怎么用python代码实现爬取奥特曼图片

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

怎么用python代码实现爬取奥特曼图片

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

怎么用python代码实现爬取奥特曼图片

完整代码

import requests
import parsel
import os
 
dirname = "奥特曼"
if not os.path.exists(dirname):     #判断是否存在名称为奥特曼的文件夹,没有就创建
    os.mkdir(dirname)
 
 
def get_response(html_url):
    headers = {
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36'
    }
 
    response = requests.get(url = html_url,headers=headers)
    return response
 
url = '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')  #获取三个时代的网页链接
 
for period_href in period_hrefs:
    period_href = 'http://www.ultramanclub.com/allultraman/' + period_href.get()
 
    period_html = get_response(period_href).text
    period_selector = parsel.Selector(period_html)
    lis = period_selector.xpath('//div[@class="ultraheros-Contents_Generations"]/div/ul/li/a/@href')
    for li in lis:
        li = 'http://www.ultramanclub.com/allultraman/' + li.get().replace('./','')     #获取每个奥特曼的网址
        # print(li)
        li_html = get_response(li).text
        li_selector = parsel.Selector(li_html)
        url = li_selector.xpath('//div[@class="left"]/figure/img/@src').get()
        # print(url)
 
        if url:
            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).content
            with open(f'{dirname}\\{png_title}.png','wb') as f:
                f.write(png_content)
            print(png_title,'图片下载完成')
        else:
            continue

怎么用python代码实现爬取奥特曼图片

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

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

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

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI