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

刚看书学了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条)

报告相同问题?

悬赏问题

  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛