0
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'
4

1 回答 1

0

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

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

from urllib.parse import parse_qs, urlparse

def getID(url):
    query_string = urlparse(url).query
    return parse_qs(query_string)['id'][0]
于 2016-05-08T21:44:46.073 回答