首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python:AttributeError:'NoneType‘对象没有'split’属性

Python:AttributeError:'NoneType‘对象没有'split’属性
EN

Stack Overflow用户
提问于 2016-02-14 12:52:20
回答 1查看 581关注 0票数 1
代码语言:javascript
复制
 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.The代码的帮助下做html解析,但却抛出了属性错误。可能的问题是什么?如果可能的话,请帮助我。

EN

回答 1

Stack Overflow用户

发布于 2016-02-14 12:55:56

显然,user_choice密钥不在urldict字典中,并且urldict.get(user_choice)返回None。假设您使用的是Python3,并且您的urldict密钥是整数,则需要在进行查找之前将输入的数字转换为整数:

代码语言:javascript
复制
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])

此外,您可能应该更好地处理“缺少键”的情况。例如:

代码语言:javascript
复制
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])
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35388479

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档