python爬取某游戏皮肤(学习使用)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python爬取某游戏皮肤(学习使用)相关的知识,希望对你有一定的参考价值。

        使用到了requests,xpath,re解析模块,同时使用了time,os模块辅助。使用xpath解析时,并不是所见即所得,没有re好用。

一、requests模块:

url = "https://pvp.qq.com/web201605/herolist.shtml"
headers =
"User-Agent": "Mozilla/5.0(Windows NT 6.1;WOW64) AppleWebKit/537.36(KABUL, like Gecko) "
"Chrome/86.0.4240.198Safari/537.36 "

resp = requests.get(url=url, headers=headers)
resp.encoding = resp.apparent_encoding


二、xpath模块

from lxml import etree

e = etree.HTML(resp.text)
href = e.xpath("//div[@class=herolist-box]/div/ul/li/a/@href")
names = e.xpath("//div[@class=herolist-box]/div/ul/li/a/img/@alt")


三、re模块

import re

reg = rbackground:url\\(\\.*?\\\\)
src = re.findall(reg, resp_1.text, re.S)


四、os和time模块

import time
import os

if not os.path.exists(03-herodetail):
os.makedirs(03-herodetail)

time.sleep(2)


五、图片文件保存:

with open(f03-herodetail/i.jpg, "wb") as f:
f.write(resp.content)


六、完整实例:


import requests
from lxml import etree
import time
import os
import re

if not os.path.exists(03-herodetail):
os.makedirs(03-herodetail)
url = "https://pvp.qq.com/web201605/herolist.shtml"
headers =
"User-Agent": "Mozilla/5.0(Windows NT 6.1;WOW64) AppleWebKit/537.36(KABUL, like Gecko) "
"Chrome/86.0.4240.198Safari/537.36 "

resp = requests.get(url=url, headers=headers)
resp.encoding = resp.apparent_encoding
e = etree.HTML(resp.text)
href = e.xpath("//div[@class=herolist-box]/div/ul/li/a/@href")
names = e.xpath("//div[@class=herolist-box]/div/ul/li/a/img/@alt")
# for name in names:
# print(name)
lst_link = []
for link in href:
lst_link.append("https://pvp.qq.com/web201605/"+ link)
# print(lst_link)

for item in lst_link:
print(item)
resp_1 = requests.get(item, headers=headers)
resp_1.encoding = resp_1.apparent_encoding
# print(resp_1.status_code)
# print(resp_1.text)
# break
e_1 = etree.HTML(resp_1.text)
data_title = e_1.xpath("//div[@class=zk-con1 zk-con]/div/div/div/ul/@data-imgname")

# print(data_title,type(data_title))
# break
# bi_zhi_url = e_1.xpath("//div[3]/div[1]/@style")
data_src = e_1.xpath("//div[@class=zk-con1 zk-con]/div/div/div/ul/li//@src")
reg = rbackground:url\\(\\.*?\\\\)
src = re.findall(reg, resp_1.text, re.S)
n = src[0].split(//)
# print(n)
# print(type(n))
name = n[1][:-7]
# break
# data_name = e_1.xpath("//div[@class=zk-con1 zk-con]/div/div/div/ul/@data-imgname")
# print(data_name)
names = data_title[0].split(|)
count = 0
for i in names:
count += 1
# print(i)
src_1 = src
href = "http://" + name + str(count) + ".jpg"
resp = requests.get(url=href, headers=headers)
with open(f03-herodetail/i.jpg, "wb") as f:
f.write(resp.content)
time.sleep(2)


七:效果截图:

python爬取某游戏皮肤(学习使用)_html





以上是关于python爬取某游戏皮肤(学习使用)的主要内容,如果未能解决你的问题,请参考以下文章

Python项目实战:带领你爬取某主播的人气指数和昵称

王者荣耀五周年,爬取102个英雄+326款皮肤,分析上线时间

用Python爬取英雄联盟(lol)全部皮肤

python爬虫--爬取某网站电影下载地址

20行Python代码爬取王者荣耀全英雄皮肤

Python练手项目:20行爬取全王者全英雄皮肤