不要葱姜蒜_kmon4 2021-01-11 10:40 采纳率: 100%
浏览 274
已采纳

刚看书学了python的爬虫,在爬取网页的时候遇到了“超出索引范围的问题”,该怎么解决

下面的代码

import requests
import re


"""
https://www.xsbiquge.com/20_20331/
https://www.xsbiquge.com/20_20331/1135932.html
</a></dd><dd><a href="/20_20331/1135938.html">
"""
url = 'https://www.xsbiquge.com/20_20331/'
headers = {
    'Host': 'www.xsbiquge.com',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'
}

response = requests.get(url=url, headers=headers)
response.encoding = response.apparent_encoding
html_data = response.text
# print(html_data)
fiction_zj_url_list = re.findall('<dd><a href="(.*?)">', html_data, re.S)
print(fiction_zj_url_list)
try:
    for page in range(0, 1, 1):
        print('https://www.xsbiquge.com/20_20331{}'.format(fiction_zj_url_list[page]))
        fiction_zj_url = 'https://www.xsbiquge.com/20_20331{}'.format(fiction_zj_url_list[page])
        response_2 = requests.get(url=fiction_zj_url, headers=headers)
        response_2.encoding = response_2.apparent_encoding
        html_data_2 = response_2.text
        result_title = re.findall('<h1>(.*?)</h1>', html_data_2, re.S)
        result_zw = re.findall('<div id="content">(.*?)</div>', html_data_2, re.S)
        zw_data = result_zw[0].replace('&nbsp;', '').replace('<br />', '')
        with open('万古神帝.txt', mode='a', encoding='utf-8') as f:
            f.write(result_title[0])
            f.write(zw_data)
except Exception as e:
    print(e)
    pass
  • 写回答

3条回答 默认 最新

  • PythonJavaC++go 2021-01-11 12:33
    关注

    有输出,就是你后面使用的时候索引超出改列表上限了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容