索引越界是因为按照正则式没有匹配到结果,获取数据为空。1.将Spider.root_pattern,改写成root_pattrern,2.检查root_pattern正则式是否正确。
先不要写成函数,获取页面,解析,改一下正则,这样这可获取到相应的页面内容。参考代码:
import re
from urllib.request import urlopen
url='https://www.huya.com/g/wzry'
r=urlopen(url).read().decode()
print(r)
root_pattern='<span class="txt">[\s\S](.*?)</div>'
root_html=re.findall(root_pattern,r,re.S)
title = re.findall('<i class="nick" title=(.*?)>', r)
print(root_html)
print(title)
root_html没有下标为3的元素,越界了。
直接print(root_html)看看先