AttributeError:'ParseResult'对象没有属性'id'

时间:2016-05-08 21:39:07

标签: python python-3.x url attributeerror urlparse

def getPeopleLinks(page):
    links = []
    for link in page.find_all('a'):
        url = link.get('href')
        if url:
            if 'profile/view?id=' in url:
                links.append(url)
    return links

def getID(url):
    pUrl = urlparse(url)
    return pUrl.id

从下面执行

people = getPeopleLinks(page)
if people:
    for person in people:
        ID = getID(person)

当我执行它时,我收到此错误:

   return pUrl.id
AttributeError: 'ParseResult' object has no attribute 'id'

1 个答案:

答案 0 :(得分:0)

urlparse()函数不会将查询字符串解析为键值对;它只返回URL的组成部分(包括查询字符串部分,还包括方案,主机名,片段标识符等)。

从URL中提取后,您需要解析查询字符串;使用urllib.parse.parse_qs() function

from urllib.parse import parse_qs, urlparse

def getID(url):
    query_string = urlparse(url).query
    return parse_qs(query_string)['id'][0]