Paython微博根据用户名搜索爬取该用户userId

根据微博用户名搜索爬取该用户userId并返回


import urllib.request
from urllib import parse
 
from bs4 import BeautifulSoup
 
 
# 微博根据用户名查找userId
 
 
# userName 用户名
# pageNum 查询页数,每页20个数据。 默认为第一页值为1,第二页值为2,以此类推。
def getUserId(userName, pageNum):
    # 用户名需要URL编码后
    html_doc = "https://s.weibo.com/user/&nickname=" + parse.quote(userName) + "&page=" + pageNum
    req = urllib.request.Request(html_doc)
    webpage = urllib.request.urlopen(req)
    html = webpage.read()
    soup = BeautifulSoup(html, 'html.parser')  # 文档对象
    if soup:
        print("找到html")
    # 第一步:抓取a标签
    # 暴烈甜心鳄鱼毛毛
    for a in soup.find_all('a', class_='name'):
        if a:
            # 第二步:抓取a标签中用户名
            rpuserName = a.get_text()
            print("搜到用户名=" + rpuserName)
            # 第三步: 判断是否有该用户,如果有,获取该用户userId
            if a.get_text(strip=True) == userName:
                print("匹配到该用户")
                print("用户个人主页链接=" + a['href'])
                # 第四步:提取userId,然后返回
                userUrl = a['href'].split("/")
                print(userUrl)
                if userUrl and len(userUrl) > 0:
                    userId = userUrl[len(userUrl) - 1]
                    return "userId=" + userId
                else:
                    return "userID抓取失败"
                break
        else:
            return "没有查找到a标签"
    else:
        return "没有找到数据"
 
 
if __name__ == "__main__":
    userName = "用户名"
    print(getUserId(userName, "1"))

你可能感兴趣的:(python)