繁体   English   中英

在Python中尝试Exception for pop

[英]Try Exception for pop in 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

谢谢!!!

使用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)

第一点,在这里:

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

表达式random.randint(1, len(list_links))将返回一个介于1和len(list_links)之间的int,但是(像大多数其他语言的FWIW一样)python列表索引从零开始,因此您需要random.randint(0, len(list_links) - 1)

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

编辑

另外,您的代码比需要的复杂得多。 如果您想要的是3个来自list_links随机不同的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个min(3, len(linked_lists)但这并不需要:每次发现自己编写时somevar1, somevar2, somevarN确实确实需要一个列表(或其他序列类型),在这种情况下,调用者代码应类似于:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM