在Python中尝试Exception for pop

时间:2018-10-11 11:12:18

标签: python

t1.py用于主程序

t3.py用于子程序

好像网站有问题 并非总是如此,但有时可能会在主程序中遇到以下错误 有什么解决办法吗?

------错误消息--------

res = list_links.pop(random.randint(1,len(list_links)))#根据列表的大小随机弹出每个项目

IndexError:弹出索引超出范围


t1.py

# -*- coding: utf-8 -*-
"""
Created on Fri Oct  5 21:51:34 2018

@author: chevady
"""
from linebot.models import *
from t3 import apple_newsss
apple_newss_content0, apple_newss_content1, apple_newss_content2 = apple_newsss()

print(apple_newss_content0)

print(apple_newss_content1)

print(apple_newss_content2)

t3.py

# -*- coding: utf-8 -*-
import requests
import re
import os
import random
from bs4 import BeautifulSoup
from flask import Flask, request, abort
#from imgurpython import ImgurClient
from argparse import ArgumentParser

def apple_newsss():

    target_url = 'http://www.appledaily.com.tw/realtimenews/section/new/'
    print('Start parsing appleNews....')
    rs = requests.session()
    res = rs.get(target_url, verify=False)
    soup = BeautifulSoup(res.text, 'html.parser')

    list_links = [] # Create empty list

    for a in soup.select("div[class='abdominis rlby clearmen']")[0].findAll(href=True): # find links based on div
            if a['href']!= None and a['href'].startswith('https://'):
                    list_links.append(a['href']) #append to the list
                    print(a['href']) #Check links

#for l in list_links: # print list to screen (2nd check)
#    print(l)


    print("\n")

    random_list = [] #create random list if needed..
    random.shuffle(list_links) #random shuffle the list
    apple_newss_content0 = ''
    apple_newss_content1 = ''
    apple_newss_content2 = ''
    for i in range(3): # specify range (5 items in this instance)
        res = list_links.pop(random.randint(1, len(list_links))) # pop of each item randomly based on the size of the list
        random_list.append(res)
    #print(res)
    #print(random_list)
    print("\n")
    apple_newss_content0=random_list[0]
    apple_newss_content1=random_list[1]
    apple_newss_content2=random_list[2]

    print(apple_newss_content0)
    print(apple_newss_content1)
    print(apple_newss_content2)

    return apple_newss_content0, apple_newss_content1, apple_newss_content2

谢谢!

2 个答案:

答案 0 :(得分:2)

使用len(list_links)-1

例如:

for i in range(3): # specify range (5 items in this instance)
    res = list_links.pop(random.randint(1, len(list_links)-1)) # pop of each item randomly based on the size of the list
    random_list.append(res)

答案 1 :(得分:1)

第一点,在这里:

res = list_links.pop(random.randint(1, len(list_links)))

表达式random.randint(1, len(list_links))将返回一个介于1到len(list_links)之间的整数,但是(像大多数其他语言的FWIW一样),python列表索引是从零开始的,所以您希望random.randint(0, len(list_links) - 1)

现在这不能完全解决您的问题,因为您不检查list_links中是否至少包含3个项目,因此,如果它为空或在for循环中变空,您仍然会收到错误消息

编辑

此外,您的代码比需要的复杂得多。如果您想要的是来自list_links的3个随机不同的URL,那么您所需要做的就是:

# make sure we only have distinct urls
list_links = list(set(list_links))
# make sure the urls are in arbitrary order
random.shuffle(list_links) 
# returns the x (x <= 3) first urls 
return list_links[:3]

请注意,这段代码实际上可能返回少于三个url(实际上将返回min(3, len(linked_lists)个url,因此调用者不应该依赖于总是返回3个url-但这始终不是必需的:每次您发现自己编写时somevar1, somevar2, somevarN确实确实想要一个列表(或其他某种序列类型)。在这种情况下,调用者代码应如下所示:

urls = apple_newsss()
for url in urls:
    print(url)