jeudi 27 juillet 2017

Couldn't Iterate The List More in Python 3

After several searching and modifies, I still couldn't figure it out. It stops at the 19th in the list.

This is the error:

line 22 in <module> parent_td=[td for td in possible_tds if 'Product' in td.text][0]
Index Error: List index out of range


    from urllib.request import urlopen as uReq
    from urllib.request import Request
    from bs4 import BeautifulSoup as soup
    import re

    ListName = "list"
    OpenList = open(ListName,"r")
    n=int(OpenList.readline())

    for num in range(1,n+1):
         print(num)
         theList=OpenList.readline()

         AccessCME=Request(theList,headers={"User-Agent":"Mozilla/5.0"})
         CMEPage=uReq(AccessCME).read()

         page_soup=soup(CMEPage,"html.parser")
         cme=page_soup.find("div",{"class":"cmeProduct section"})
         FuturesContracts=cme.span.text.strip()

         possible_tds=page_soup.find_all('td',attrs={'class':"prodSpecAtribute"})
         parent_td=[td for td in possible_tds if 'Product' in td.text][0]
         target = parent_td.fetchNextSiblings('td')[0].text.strip()
         first_take=re.sub('CME Globex:\s', '', target)
         BaseSymbol=re.sub(r'CME ClearPort:.*', '', first_take)

         print(FuturesContracts, BaseSymbol)

The link is the list file:

http://ift.tt/2tFWPkD

Let me know if I need to clarify anything else. Improvements/suggestions are welcome.




Aucun commentaire:

Enregistrer un commentaire