Paython Weibo crawls the user userid according to the user name search


Search and crawl the userid of the user according to the microblog user name and return

import urllib.request
from urllib import parse
from bs4 import BeautifulSoup
#Weibo finds userid according to user name
#Username user name
#Pagenum queries the number of pages, 20 data per page. The default value is 1 for the first page, 2 for the second page, and so on.
def getUserId(userName, pageNum):
    #After the user name needs URL encoding
    html_doc = "" + parse.quote(userName) + "&page=" + pageNum
    req = urllib.request.Request(html_doc)
    webpage = urllib.request.urlopen(req)
    html =
    Soup = beautifulsop (HTML, 'HTML. Parser') (document object)
    if soup:
        Print ("find HTML")
    #Step 1: grab a tag
    #Color red > crocodile < / EM > fur</a>
    for a in soup.find_all('a', class_='name'):
        if a:
            #Step 2: grab the user name in the a tag
            rpuserName = a.get_text()
            Print ("user name found =" + rpusername)
            #Step 3: judge whether there is the user, if so, obtain the userid of the user
            if a.get_text(strip=True) == userName:
                Print ("match to user")
                Print ("user's personal home page link =" + a ['href '])
                #Step 4: extract userid and return to
                userUrl = a['href'].split("/")
                if userUrl and len(userUrl) > 0:
                    userId = userUrl[len(userUrl) - 1]
                    return "userId=" + userId
                    Return "failed to retrieve userid"
            Return "no a tag found."
        Return "no data found."
if __name__ == "__main__":
    Username = "user name"
    print(getUserId(userName, "1"))