2016-02-14 159 views
1
h = soup.findAll("div", {"id": "products"}) 
for row in h: 
    b = row.findAll("div", {"class": "gd-row"}) 
    for a in b: 
     c = a.findAll("div", {"class": "gd-col"}) 
     for d in c: 
      e = d.findAll("div", {"class": "product-unit"}) 
      for f in e: 
       g = f.findAll("div", {"class": "pu-details"}) 
       for h in g: 
        i = h.findAll("div", {"class": "pu-title"}) 
        for k in i: 
         l = k.findAll('a') 
         for z in l: 
          text = z.get('href') 

          title = str(z.get_text().strip()) 
          urldict.update({counter: text}) 
          print (str(counter) + ')' + title) 

          titlelist.append(title) 

          counter = counter + 1 


print ("\nSeems we found more than one same mobile type, help us by selecting appropiate model \n") 
user_choice = input("\nEnter your choice (number) which matches exactly:") 
url_from_search = "http://www.xyzabc.com{}".format(
    urldict.get(user_choice).split('&')[0]) 

任何人都可以幫助我嗎?我正嘗試使用beautifulsoup的幫助來完成html解析。上面給出的代碼是拋出屬性錯誤。什麼可能是這個問題?請儘可能幫助我。Python:AttributeError:'NoneType'對象沒有屬性'split'

+0

當您發佈錯誤時,最好發佈堆棧跟蹤或至少發生錯誤的錯誤發生地點。 Python爲您提供堆棧跟蹤以幫助調試......所以將它傳遞給您! – tdelaney

回答

3

顯然,user_choice關鍵不在於urldict字典和urldict.get(user_choice)回報None內。假設你使用Python 3,你的urldict鍵是整數,您需要在查詢之前輸入的數字轉換成整數:

user_choice = int(input("\nEnter your choice (number) which matches exactly:")) 
url_from_search = "http://www.acxcs.com{}".format(
    urldict.get(user_choice).split('&')[0]) 

此外,你應該辦理「失蹤鍵」的美好局面。例如:

user_choice = int(input("\nEnter your choice (number) which matches exactly:")) 
if user_choice not in urldict: 
    print("Error, the number is not valid") 
else: 
    url_from_search = "http://www.acxcs.com{}".format(urldict[user_choice].split('&')[0]) 
相關問題