Javascript 如果出现错误,请重新加载页面";索引器:列表索引超出范围“;发生
我正在抓取网页,有时页面加载不正确,出现错误 索引器:列表索引超出范围 这是因为页面没有正确加载,因此没有索引。重新加载页面可以解决这个问题 如果页面未加载且出现错误,是否需要添加错误处理。。。重新加载页面 我在网上搜索过,什么也找不到Javascript 如果出现错误,请重新加载页面";索引器:列表索引超出范围“;发生,javascript,jquery,python,events,Javascript,Jquery,Python,Events,我正在抓取网页,有时页面加载不正确,出现错误 索引器:列表索引超出范围 这是因为页面没有正确加载,因此没有索引。重新加载页面可以解决这个问题 如果页面未加载且出现错误,是否需要添加错误处理。。。重新加载页面 我在网上搜索过,什么也找不到 for link in links: #print('Fetching from link: ' + link) browser.get('http://www.racingpost.com' + link) time.sleep(5)
for link in links:
#print('Fetching from link: ' + link)
browser.get('http://www.racingpost.com' + link)
time.sleep(5)
print('http://www.racingpost.com' + link)
tree = html.fromstring(browser.page_source)
#print(browser.page_source)
if count == 0:
browser.find_element_by_xpath("//*[@id='re_']/div[2]/a[1]").click()
browser.find_element_by_xpath("//*[@id='re_']/div[2]/a[2]").click()
count = count + 1
#first of all pull all the data about the event its self like going distance ect
title = tree.xpath('//*[@id="mainwrapper"]/div/div/div[2]/div[1]/div[2]/h3/text()[2]')
title = map(lambda x:x.strip(),title)
title = [x.strip(' ') for x in title]
details = tree.xpath('//*[@id="mainwrapper"]/div/div/div[2]/div[1]/div[2]/ul/li[1]/text()[1]')
prizemoney = tree.xpath('//*[@id="mainwrapper"]/div/div/div[2]/div[1]/div[2]/ul/li[2]/text()[1]')
setoff = tree.xpath('//*[@id="mainwrapper"]/div/div/div[2]/div[1]/div[2]/h3/span/text()')
course = tree.xpath('//*[@id="mainwrapper"]/div/div/div[2]/div[1]/div[2]/h1/text()[1]')
print(course)
course[0] = course[0].replace('Result', '')
date = tree.xpath('//*[@id="mainwrapper"]/div/div/div[2]/div[1]/div[2]/h1/text()[2]')
timeoff = tree.xpath('//div[@class="raceInfo"]/text()[3]')
上面是一个代码snippit->如果borwser.get没有抓取页面(服务器拒绝或超时),那么我需要重试。您可以添加一个try/exception错误,该错误将返回某种变量,告诉页面它没有正确加载,然后您可以使用javascript函数
location.reload()
重新加载页面
例如:
在Python脚本中:
try:
'Your Code Goes Here'
except IndexError:
return 'e'
在Javascript中:
if(xml.responsetext=='e'){
location.reload(true);//true if you dont want to load from cache,otherwise leave it blank
}
如果有任何错误,请报告我,因为我也尝试在我的网站上使用python脚本和AJAX,而且我对它非常陌生。我很想发表评论而不是回答它,但我的声誉不允许我这样做
干杯我认为您需要进行一些重构。 应该是这样的:
def get_page(link):
# all code stuff for fetching page
# this code could return ether error code or throw Exception
return data
for link in links:
try:
result = get_page(link)
# here you need to add this result
except IndexError:
#log this error
result = get_page(link) #this is retry. you can add slip() here too
这是一个又快又脏的示例,您可以通过更好的重试日志记录、全局计算每个链接的重试次数等来改进它。使用什么
scrapy
?如果是,那么它将尝试相同的页面三次,否则您可以使用lxml-xpath(添加到描述中-第一次在此处发布:)检查其文档是否相同。没有任何代码很难回答。但通常您需要一个位置来捕获此错误并发出重试请求。