Chevady Ju
Chevady Ju

Reputation: 43

Try Exception for pop in Python

t1.py for main program

t3.py for sub program

seems like the website have some problem not always but sometime may meet error below in main program is there any solution?

------error message--------

res = list_links.pop(random.randint(1, len(list_links))) # pop of each item randomly based on the size of the list

IndexError: pop index out of range


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

Thanks!!!

Upvotes: 0

Views: 723

Answers (2)

bruno desthuilliers
bruno desthuilliers

Reputation: 77912

First point, here:

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

The expression random.randint(1, len(list_links)) will return an int between 1 and len(list_links), but (like most other languages FWIW) python lists indices are zero-based so you'd want random.randint(0, len(list_links) - 1)

Now this will not totally solve your problem, since you don't check if list_links has at least 3 items in it, so if it's empty or becomes empty during the for loop you'll still get an error.

EDIT

Also, your code is much more complicated than it needs to be. If what you want is 3 random distinct urls from list_links, all you need is:

# 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]

Note that this code might actually return less than three urls (it will min(3, len(linked_lists) urls actually, so the caller shouldn't rely on always getting 3 urls back - but that's not needed anyway: everytime you find yourself writing somevar1, somevar2, somevarN you really want a list (or some other sequence type) indeed. In this case, the caller code should looks like:

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

Upvotes: 1

Rakesh
Rakesh

Reputation: 82785

Use len(list_links)-1

Ex:

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)

Upvotes: 1

Related Questions