Mondoz
2014-11-27 09:10
采纳率: 100%
浏览 2.2k
已采纳

Python 爬虫 index溢出

import urllib2
import re
from xlsxwriter.workbook import Workbook
from io import BytesIO
import json

workbook = Workbook('images.xlsx')
print "FETCHING YHD ..."
u = urllib2.urlopen('http://www.yhd.com/')
data = u.read()

ol_tag = re.findall(r'

.*?', data, flags = re.DOTALL)
li_tag = re.findall(r'', ol_tag[0], flags = re.DOTALL)

yhd_link = []
for item in li_tag:
link = re.findall(r'(?<=wi=").*?(?=")', item)
yhd_link += link

yhd_href_link = []
for item in li_tag:
link = re.findall(r'(?<=href=").*?(?=")', item)
yhd_href_link += link

worksheet = workbook.add_worksheet('yhd')

print "Total: " + str(len(yhd_link))
for i in range(len(yhd_link)):
print "yhd => Downloading Picture " + str(i+1)
image_data = BytesIO(urllib2.urlopen(yhd_link[i]).read())
if i % 4 == 0:
worksheet.set_row(i, 240)
else:
worksheet.set_row(i, 120)
worksheet.insert_image('A' + str(i+1), yhd_link[i], {'image_data': image_data, 'url': yhd_href_link[i]})

print "COMPLETE YHD ..."
workbook.close()

Traceback (most recent call last):
File "C:\Python27\project\banner\banneryhd.py", line 16, in
li_tag = re.findall(r'', ol_tag[0], flags = re.DOTALL)
IndexError: list index out of range

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • 传说中的虫虫 2014-12-03 03:35
    已采纳

    ol_tag = re.findall(r'.*?', data, flags = re.DOTALL)

    这里如果没有找到结果下一句话里的ol_tag[0] 就会导致索引错误, 所以你应该加保护语句, 没找到就给出相应的提示

    li_tag = re.findall(r'', ol_tag[0], flags = re.DOTALL)

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题