简体   繁体   中英

Python:AttributeError: 'NoneType' object has no attribute 'split'

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

Can anyone help me with this? I am trying to do html parsing with the help of beautifulsoup.The code given above is throwing attribute error. What might be the issue? Kindly help me if possible.

Apparently, user_choice key is not inside the urldict dictionary and urldict.get(user_choice) returns None . Assuming you are using Python 3 and your urldict keys are integers, you need to convert the input number to integer before making a lookup:

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

Also, you should probably handle the "missing key" situation better. For example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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