|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
基于Tom.Error前辈的Python爬取LOL全英雄技能介绍文章写下自己的爬虫
直接将代码贴出来,求各位指点迷津,
- import requests,time,re
- def now_to_timestamp(digits = 13):#生成13位的时间戳
- time_stamp = time.time()
- digits = 10 ** (digits -10)
- time_stamp = int(round(time_stamp*digits))
- return time_stamp
- def open_url():
- headers = {
- 'Referer': 'https://daoju.qq.com/lol/list/shoppinglist.shtml?ADTAG=innercop.lol.SY.shoppinglist_new',
- 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'}#文件头
-
- n=[]
- for i in range(1,62):#通过查询页面获得共61页
- print("第" + str(i) + "页")#实时获得当前爬到第几页
- #通过对比,URL只有page页码和最后的13位时间戳不一致,通过format函数直接生成,并通过for循环遍历访问
- url = "https://apps.game.qq.com/daoju/v3/api/hx/goods/app/v71/GoodsListApp.php?view=biz_portal&page={}&pageSize=16&orderby=dtShowBegin&ordertype=desc&appSource=pc&plat=1&output_format=jsonp&biz=lol&_={}".format(i,now_to_timestamp(digits = 13))
- response= requests.get(url, headers=headers).text
- #基于Tom.Error前辈的Python爬取LOL全英雄技能介绍中了解的编码解码方法,原理还不是很懂,请大神指教
- res =response.encode("latin-1").decode("unicode_escape")
- m=re.findall(r'"propName":"(.*?)","busId"', res, re.S)#通过正则表达式获取皮肤的名称
- n.append(m)#不会在for循环里面保存文件,只能讲获得的结果追加到列表n 中,循环结束之后在进行保存
- time.sleep(1)#我自己的习惯,每次循环休息1S
- with open('英雄联盟皮肤列表.txt','a+',encoding = 'utf-8') as f:
- f.write("第" + str(i) + "页" + '\n' + str(m) + '\n'*3)#保存到英雄联盟皮肤列表.txt当中
- return res
-
- open_url()
- '''
- 请大神指教为什么会出现这个错误,但是又不影响运行
- Warning (from warnings module):
- File "G:\pythonwj\爬取英雄联盟商场的皮肤名称\获取英雄联盟皮肤列表.py", line 20
- res =response.encode("latin-1").decode("unicode_escape")
- DeprecationWarning: invalid escape sequence '\/'
- '''
复制代码
|
|