2016-02-14 154 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]) 
相关问题