Use Python to obtain nickname Based on Sina Weibo UID-simple crawlers and pythonuid
1 from bs4 import BeautifulSoup 2 import requests 3 from random import choice 4 import csv 5 6 headers1 = {'user-agent ': 'spider '} 7 headers2 = {'user-agent': 'spider'} 8 hehe = [headers1, headers2] 9 headers = choice (hehe) 10 11 def zhiding (id): 12 url = 'HTTP: // weibo.com/u/{}'.format (str (id) 13 data = requests. get (url, headers = headers) 14 soup = BeautifulSoup (data. text, 'lxml') 15 with open('chunxue3.csv ', 'A', encoding = 'gbk', newline = '') as csvfile: 16 spamwriter = csv. writer (csvfile, dialect = 'excel ') 17 spamwriter. writerow ([id, soup. title. get_text (). split ('') [0]) 18 19 def openflile (path): 20 fp = open (path) 21 wbids = [line for line in fp. readlines ()] 22 for wbid in wbids: 23 zhiding (wbid) 24 print ('done') 25 26 txtpath = r'e: \ nantong.txt '27 28 openflile (txtpath)