[Python3爬蟲]爬取新浪微博用戶信息及微博內(nèi)容
大數(shù)據(jù)時代,對于研究領(lǐng)域來說,數(shù)據(jù)已經(jīng)成為必不可少的一部分。新浪微博作為新時代火爆的新媒體社交平臺,擁有許多用戶行為及商戶數(shù)據(jù),因此需要研究人員都想要得到新浪微博數(shù)據(jù),But新浪微博數(shù)據(jù)量極大,獲取的最好方法無疑就是使用Python爬蟲來得到。網(wǎng)上有一些關(guān)于使用Python爬蟲來爬取新浪微博數(shù)據(jù)的教程,但是完整的介紹以及爬取用戶所有數(shù)據(jù)信息比較少,因此這里分享一篇主要通過selenium包來爬取新浪微博用戶數(shù)據(jù)的文章。 碼字不易,喜歡請點贊!!!
目標(biāo)
爬取新浪微博用戶數(shù)據(jù),包括以下字段:id,昵稱,粉絲數(shù),關(guān)注數(shù),微博數(shù),每一篇微博的內(nèi)容,轉(zhuǎn)發(fā)數(shù),評論數(shù),點贊數(shù),發(fā)布時間,來源,以及是原創(chuàng)還是轉(zhuǎn)發(fā)。(本文以GUCCI(古馳)為例)
方法
- 使用selenium模擬爬蟲
- 使用BeautifulSoup解析HTML
結(jié)果展示
步驟分解
1.選取爬取目標(biāo)網(wǎng)址
首先,在準(zhǔn)備開始爬蟲之前,得想好要爬取哪個網(wǎng)址。新浪微博的網(wǎng)址分為網(wǎng)頁端和手機端兩個,大部分爬取微博數(shù)據(jù)都會選擇爬取手機端,因為對比起來,手機端基本上包括了所有你要的數(shù)據(jù),并且手機端相對于PC端是輕量級的。
下面是GUCCI的手機端和PC端的網(wǎng)頁展示。
2.模擬登陸
定好爬取微博手機端數(shù)據(jù)之后,接下來就該模擬登陸了。
模擬登陸的網(wǎng)址
登陸的網(wǎng)頁下面的樣子
模擬登陸代碼
try:
print(u'登陸新浪微博手機端...')
##打開Firefox瀏覽器
browser = webdriver.Firefox()
##給定登陸的網(wǎng)址
url = 'https://passport.weibo.cn/signin/login'
browser.get(url)
time.sleep(3)
#找到輸入用戶名的地方,并將用戶名里面的內(nèi)容清空,然后送入你的賬號
username = browser.find_element_by_css_selector('#loginName')
time.sleep(2)
username.clear()
username.send_keys('****')#輸入自己的賬號
#找到輸入密碼的地方,然后送入你的密碼
password = browser.find_element_by_css_selector('#loginPassword')
time.sleep(2)
password.send_keys('ll117117')
#點擊登錄
browser.find_element_by_css_selector('#loginAction').click()
##這里給個15秒非常重要,因為在點擊登錄之后,新浪微博會有個九宮格驗證碼,下圖有,通過程序執(zhí)行的話會有點麻煩(可以參考崔慶才的Python書里面有解決方法),這里就手動
time.sleep(15)
except:
print('########出現(xiàn)Error########')
finally:
print('完成登陸!')
3.獲取用戶微博頁碼
在登錄之后可以進入想要爬取的商戶信息,因為每個商戶的微博量不一樣,因此對應(yīng)的微博頁碼也不一樣,這里首先將商戶的微博頁碼爬下來。與此同時,將那些公用信息爬取下來,比如用戶uid,用戶名稱,微博數(shù)量,關(guān)注人數(shù),粉絲數(shù)目。
#本文是以GUCCI為例,GUCCI的用戶id為‘GUCCI’
id = 'GUCCI'
niCheng = id
#用戶的url結(jié)構(gòu)為 url = 'http://weibo.cn/' + id
url = 'http://weibo.cn/' + id
browser.get(url)
time.sleep(3)
#使用BeautifulSoup解析網(wǎng)頁的HTML
soup = BeautifulSoup(browser.page_source, 'lxml')
#爬取商戶的uid信息
uid = soup.find('td',attrs={'valign':'top'})
uid = uid.a['href']
uid = uid.split('/')[1]
#爬取最大頁碼數(shù)目
pageSize = soup.find('div', attrs={'id': 'pagelist'})
pageSize = pageSize.find('div').getText()
pageSize = (pageSize.split('/')[1]).split('頁')[0]
#爬取微博數(shù)量
divMessage = soup.find('div',attrs={'class':'tip2'})
weiBoCount = divMessage.find('span').getText()
weiBoCount = (weiBoCount.split('[')[1]).replace(']','')
#爬取關(guān)注數(shù)量和粉絲數(shù)量
a = divMessage.find_all('a')[:2]
guanZhuCount = (a[0].getText().split('[')[1]).replace(']','')
fenSiCount = (a[1].getText().split('[')[1]).replace(']', '')
4.根據(jù)爬取的最大頁碼,循環(huán)爬取所有數(shù)據(jù)
在得到最大頁碼之后,直接通過循環(huán)來爬取每一頁數(shù)據(jù)。抓取的數(shù)據(jù)包括,微博內(nèi)容,轉(zhuǎn)發(fā)數(shù)量,評論數(shù)量,點贊數(shù)量,發(fā)微博的時間,微博來源,以及是原創(chuàng)還是轉(zhuǎn)發(fā)。
#通過循環(huán)來抓取每一頁數(shù)據(jù)
for i in range(1, pageSize+1): # pageSize+1
#每一頁數(shù)據(jù)的url結(jié)構(gòu)為 url = 'http://weibo.cn/' + id + ‘?page=’ + i
url = 'https://weibo.cn/GUCCI?page=' + str(i)
browser.get(url)
time.sleep(1)
#使用BeautifulSoup解析網(wǎng)頁的HTML
soup = BeautifulSoup(browser.page_source, 'lxml')
body = soup.find('body')
divss = body.find_all('div', attrs={'class': 'c'})[1:-2]
for divs in divss:
# yuanChuang : 0表示轉(zhuǎn)發(fā),1表示原創(chuàng)
yuanChuang = '1'#初始值為原創(chuàng),當(dāng)非原創(chuàng)時,更改此值
div = divs.find_all('div')
#這里有三種情況,兩種為原創(chuàng),一種為轉(zhuǎn)發(fā)
if (len(div) == 2):#原創(chuàng),有圖
#爬取微博內(nèi)容
content = div[0].find('span', attrs={'class': 'ctt'}).getText()
aa = div[1].find_all('a')
for a in aa:
text = a.getText()
if (('贊' in text) or ('轉(zhuǎn)發(fā)' in text) or ('評論' in text)):
#爬取點贊數(shù)
if ('贊' in text):
dianZan = (text.split('[')[1]).replace(']', '')
#爬取轉(zhuǎn)發(fā)數(shù)
elif ('轉(zhuǎn)發(fā)' in text):
zhuanFa = (text.split('[')[1]).replace(']', '')
#爬取評論數(shù)目
elif ('評論' in text):
pinLun = (text.split('[')[1]).replace(']', '')
#爬取微博來源和時間
span = divs.find('span', attrs={'class': 'ct'}).getText()
faBuTime = str(span.split('來自')[0])
laiYuan = span.split('來自')[1]
#和上面一樣
elif (len(div) == 1):#原創(chuàng),無圖
content = div[0].find('span', attrs={'class': 'ctt'}).getText()
aa = div[0].find_all('a')
for a in aa:
text = a.getText()
if (('贊' in text) or ('轉(zhuǎn)發(fā)' in text) or ('評論' in text)):
if ('贊' in text):
dianZan = (text.split('[')[1]).replace(']', '')
elif ('轉(zhuǎn)發(fā)' in text):
zhuanFa = (text.split('[')[1]).replace(']', '')
elif ('評論' in text):
pinLun = (text.split('[')[1]).replace(']', '')
span = divs.find('span', attrs={'class': 'ct'}).getText()
faBuTime = str(span.split('來自')[0])
laiYuan = span.split('來自')[1]
#這里為轉(zhuǎn)發(fā),其他和上面一樣
elif (len(div) == 3):#轉(zhuǎn)發(fā)的微博
yuanChuang = '0'
content = div[0].find('span', attrs={'class': 'ctt'}).getText()
aa = div[2].find_all('a')
for a in aa:
text = a.getText()
if (('贊' in text) or ('轉(zhuǎn)發(fā)' in text) or ('評論' in text)):
if ('贊' in text):
dianZan = (text.split('[')[1]).replace(']', '')
elif ('轉(zhuǎn)發(fā)' in text):
zhuanFa = (text.split('[')[1]).replace(']', '')
elif ('評論' in text):
pinLun = (text.split('[')[1]).replace(']', '')
span = divs.find('span', attrs={'class': 'ct'}).getText()
faBuTime = str(span.split('來自')[0])
laiYuan = span.split('來自')[1]
time.sleep(2)
print(i)
4.在得到所有數(shù)據(jù)之后,可以寫到csv文件,或者excel
最后的結(jié)果顯示在上面展示啦!!!!
到這里完整的微博爬蟲就解決啦!!!
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

