python爬取守望先锋英雄图片以及描述

先爬取英雄图片

导入库

import requests
import re
from bs4 import BeautifulSoup 
import urllib

爬取网页html

headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36 Edg/94.0.992.47'
    } 
def get_html(url): 
 # 模拟浏览器访问
    response = requests.get(url, headers=headers)  # 访问网站
    response.encoding = response.apparent_encoding #字符编码格式
    html = response.text  # 获取网页源码
    return html

 再找出html里面所有以png结尾的图片链接,保存到本地

r = get_html('https://ow.blizzard.cn/heroes/')
bs=BeautifulSoup(r,'html.parser')
#正则表达式爬取img以png结尾的链接
img_urls=bs.find_all('img', {'src': re.compile('.+(.png)$')})
for i in range(len(img_urls)):
    url = img_urls[i]['src']
    # 下载图片
    urllib.request.urlretrieve(url, 'C:/Users/15584/Desktop/overwatch/picture/' +"hero"+str(i) + '.jpg')

再获取每个英雄的具体描述。

首先找出所有的内链接并且以/heroes开头

#正则表达式爬取以/heroes开头的内链接
for link in bs.find_all('a',href=re.compile('^((\/heroes.))')):
    a.append(link.text)
    if 'href' in link.attrs:
        b.append(link['href'])

再访问内链接,找出所有的p标签

url="https://ow.blizzard.cn"
for link1 in b:
    c.append(url+link1)
for link2 in c:
    r1 = get_html(link2)
    bs1=BeautifulSoup(r1,'html.parser')
    #print(bs1.p.text)
    d.append(bs1.find_all('p'))

再对找出来的p标签进行去除多余的html

for content in d:
    content = str(content)
    # 使用正则表达式匹配找到的文本内容中的所有<.*?>的标签
    pattern = re.compile(r'<.*?>|\n|\s', re.S)
    # 将匹配到的内容用空来替换
    content_list = pattern.sub('', content)
    e.append(content_list)
#删除列表中第一个元素

最后保存为文件

#删除列表中第一个元素
del(e[0])
#依次保存文件
for j in range(len(e)):    
    with open("C:\\Users\\15584\\Desktop\\overwatch\\hero describe\\"+"hero"+str(j)+'.txt', mode='w', encoding='utf-8') as f:
        f.write(e[j])

结果展示如下:

会在overwatch下产生两个文件夹

产看picture为英雄图片,hero describe为英雄描述

 

随机查看一个英雄的描述

完整代码如下:

(代码比较粗糙,还需后期修改)

import requests
import re
from bs4 import BeautifulSoup 
import urllib
headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36 Edg/94.0.992.47'
    } 
def get_html(url): 
 # 模拟浏览器访问
    response = requests.get(url, headers=headers)  # 访问网站
    response.encoding = response.apparent_encoding #字符编码格式
    html = response.text  # 获取网页源码
    return html  
a=[]
b=[]
c=[]
d=[]
e=[]
r = get_html('https://ow.blizzard.cn/heroes/')
bs=BeautifulSoup(r,'html.parser')
#正则表达式爬取img以png结尾的链接
img_urls=bs.find_all('img', {'src': re.compile('.+(.png)$')})
for i in range(len(img_urls)):
    url = img_urls[i]['src']
    # 下载图片
    urllib.request.urlretrieve(url, 'C:/Users/15584/Desktop/overwatch/picture/' +"hero"+str(i) + '.jpg')
#正则表达式爬取以/heroes开头的内链接
for link in bs.find_all('a',href=re.compile('^((\/heroes.))')):
    a.append(link.text)
    if 'href' in link.attrs:
        b.append(link['href'])
url="https://ow.blizzard.cn"
for link1 in b:
    c.append(url+link1)
for link2 in c:
    r1 = get_html(link2)
    bs1=BeautifulSoup(r1,'html.parser')
    #print(bs1.p.text)
    d.append(bs1.find_all('p'))
for content in d:
    content = str(content)
    # 使用正则表达式匹配找到的文本内容中的所有<.*?>的标签
    pattern = re.compile(r'<.*?>|\n|\s', re.S)
    # 将匹配到的内容用空来替换
    content_list = pattern.sub('', content)
    e.append(content_list)
#删除列表中第一个元素
del(e[0])
#依次保存文件
for j in range(len(e)):    
    with open("C:\\Users\\15584\\Desktop\\overwatch\\hero describe\\"+"hero"+str(j)+'.txt', mode='w', encoding='utf-8') as f:
        f.write(e[j])
  • 9
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值