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)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
ol_tag = re.findall(r'.*?', data, flags = re.DOTALL)
這裡如果沒有找到結果下一句話裡的ol_tag[0] 就會導致索引錯誤, 所以你應該加保護語句, 沒找到就給出相應的提示
li_tag = re.findall(r'', ol_tag[0], flags = re.DOTALL)