利用Python根據新浪微博UID獲取昵稱-簡單小爬蟲


 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)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM