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-... ...
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)