Python selenium抓取微博內(nèi)容的示例代碼
Selenium簡(jiǎn)介與安裝
Selenium是什么?
Selenium也是一個(gè)用于Web應(yīng)用程序測(cè)試的工具。Selenium測(cè)試直接運(yùn)行在瀏覽器中,就像真正的用戶(hù)在操作一樣。支持的瀏覽器包括IE、Mozilla Firefox、Mozilla Suite等。
安裝
直接使用pip命令安裝即可!
pip install selenium
Python抓取微博有兩種方式,一是通過(guò)selenium自動(dòng)登錄后從頁(yè)面直接爬取,二是通過(guò)api。
這里采用selenium的方式。
程序:
from selenium import webdriver import time import re #全局變量 driver = webdriver.Chrome("C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe") def loginWeibo(username, password): driver.get('https://passport.weibo.cn/signin/login') time.sleep(1) driver.find_element_by_id("loginName").send_keys("haishu_zheng@163.com") driver.find_element_by_id("loginPassword").send_keys("Weibo01061122") time.sleep(1) driver.find_element_by_id("loginAction").click() #driver.close() def visitUserPage(userId): driver.get('http://weibo.cn/' + userId) print('********************') print('用戶(hù)資料') # 1.用戶(hù)id print('用戶(hù)id:' + userId) # 2.用戶(hù)昵稱(chēng) strName = driver.find_element_by_xpath("http://div[@class='ut']") strlist = strName.text.split(' ') nickname = strlist[0] print('昵稱(chēng):' + nickname) # 3.微博數(shù)、粉絲數(shù)、關(guān)注數(shù) strCnt = driver.find_element_by_xpath("http://div[@class='tip2']") pattern = r"\d+\.?\d*" # 匹配數(shù)字,包含整數(shù)和小數(shù) cntArr = re.findall(pattern, strCnt.text) print(strCnt.text) print("微博數(shù):" + str(cntArr[0])) print("關(guān)注數(shù):" + str(cntArr[1])) print("粉絲數(shù):" + str(cntArr[2])) print('\n********************') # 4.將用戶(hù)信息寫(xiě)到文件里 with open("weibo.txt", "w", encoding = "gb18030") as file: file.write("用戶(hù)ID:" + userId + '\r\n') file.write("昵稱(chēng):" + nickname + '\r\n') file.write("微博數(shù):" + str(cntArr[0]) + '\r\n') file.write("關(guān)注數(shù):" + str(cntArr[1]) + '\r\n') file.write("粉絲數(shù):" + str(cntArr[2]) + '\r\n') # 5.獲取微博內(nèi)容 # http://weibo.cn/ + userId + ? filter=0&page=1 # filter為0表示全部,為1表示原創(chuàng) print("微博內(nèi)容") pageList = driver.find_element_by_xpath("http://div[@class='pa']") print(pageList.text) pattern = r"\d+\d*" # 匹配數(shù)字,只包含整數(shù) pageArr = re.findall(pattern, pageList.text) totalPages = pageArr[1] # 總共有多少頁(yè)微博 print(totalPages) pageNum = 1 # 第幾頁(yè) numInCurPage = 1 # 當(dāng)前頁(yè)的第幾條微博內(nèi)容 contentPath = "http://div[@class='c'][{0}]" while(pageNum <= 3): #while(pageNum <= int(totalPages)): contentUrl = "http://weibo.cn/" + userId + "?filter=0&page=" + str(pageNum) driver.get(contentUrl) content = driver.find_element_by_xpath(contentPath.format(numInCurPage)).text # print("\n" + content) # 微博內(nèi)容,包含原創(chuàng)和轉(zhuǎn)發(fā) if "設(shè)置:皮膚.圖片.條數(shù).隱私" not in content: numInCurPage += 1 with open("weibo.txt", "a", encoding = "gb18030") as file: file.write("\r\n" + "\r\n" + content) # 將微博內(nèi)容逐條寫(xiě)到weibo.txt中 else: pageNum += 1 # 抓取新一頁(yè)的內(nèi)容 numInCurPage = 1 # 每一頁(yè)都是從第1條開(kāi)始抓 if __name__ == '__main__': username = 'haishu_zheng@163.com' # 輸入微博賬號(hào) password = 'Weibo01061122' # 輸入密碼 loginWeibo(username, password) # 要先登錄,否則抓取不了微博內(nèi)容 time.sleep(3) uid = 'xywyw' # 尋醫(yī)問(wèn)藥 visitUserPage(uid)
運(yùn)行結(jié)果:
同時(shí)還生成了weibo.txt文件,內(nèi)容如下
這種方法有個(gè)缺陷,就是爬取較多內(nèi)容會(huì)被封IP:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 淺談python爬蟲(chóng)使用Selenium模擬瀏覽器行為
- Python爬蟲(chóng)使用Selenium+PhantomJS抓取Ajax和動(dòng)態(tài)HTML內(nèi)容
- selenium+python設(shè)置爬蟲(chóng)代理IP的方法
- python爬蟲(chóng)系列Selenium定向爬取虎撲籃球圖片詳解
- python利用selenium進(jìn)行瀏覽器爬蟲(chóng)
- python3通過(guò)selenium爬蟲(chóng)獲取到dj商品的實(shí)例代碼
- C#使用Selenium+PhantomJS抓取數(shù)據(jù)
- 學(xué)習(xí)Python selenium自動(dòng)化網(wǎng)頁(yè)抓取器
- 使用selenium抓取淘寶的商品信息實(shí)例
- 利用selenium爬蟲(chóng)抓取數(shù)據(jù)的基礎(chǔ)教程
相關(guān)文章
Django超詳細(xì)講解圖書(shū)管理系統(tǒng)的實(shí)現(xiàn)
前段時(shí)間翻文件發(fā)現(xiàn)了以前學(xué)習(xí)python和django時(shí)做的一個(gè)系統(tǒng),當(dāng)時(shí)的想法是將這玩意做出來(lái)應(yīng)付web開(kāi)發(fā)大作業(yè)、課程設(shè)計(jì)作業(yè)甚至是畢設(shè)用的,實(shí)際上也確實(shí)應(yīng)付了課程設(shè)計(jì),功能雖然不算多,但是應(yīng)付課程設(shè)計(jì)或者大作業(yè)綽綽有余了2022-07-07Python2與Python3的區(qū)別實(shí)例分析
這篇文章主要介紹了Python2與Python3的區(qū)別,結(jié)合實(shí)例形式分析了Python2與Python3在輸出、編碼、函數(shù)、運(yùn)算等操作的常見(jiàn)區(qū)別與使用技巧,需要的朋友可以參考下2019-04-04python smtplib模塊實(shí)現(xiàn)發(fā)送郵件帶附件sendmail
這篇文章主要為大家詳細(xì)介紹了python smtplib模塊實(shí)現(xiàn)發(fā)送郵件帶附件sendmail,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05python實(shí)現(xiàn)簡(jiǎn)單俄羅斯方塊
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)簡(jiǎn)單俄羅斯方塊,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03typing.Dict和Dict的區(qū)別及它們?cè)赑ython中的用途小結(jié)
當(dāng)在 Python 函數(shù)中聲明一個(gè) dictionary 作為參數(shù)時(shí),我們一般會(huì)把 key 和 value 的數(shù)據(jù)類(lèi)型聲明為全局變量,而不是局部變量。,這篇文章主要介紹了typing.Dict和Dict的區(qū)別及它們?cè)赑ython中的用途小結(jié),需要的朋友可以參考下2023-06-06關(guān)于數(shù)據(jù)分析之滾動(dòng)窗口pandas.DataFrame.rolling方法
Pandas庫(kù)中的rolling方法是數(shù)據(jù)處理中常用的功能,它允許用戶(hù)對(duì)數(shù)據(jù)進(jìn)行滾動(dòng)窗口(滑動(dòng)窗口)操作,通過(guò)指定窗口大小,可以使用不同的聚合函數(shù)對(duì)窗口內(nèi)的數(shù)據(jù)進(jìn)行計(jì)算,例如最大值、最小值、平均值、中位數(shù)等,此外,rolling方法還可以計(jì)算方差、標(biāo)準(zhǔn)差、偏度、峰度2024-09-09簡(jiǎn)單介紹Python虛擬環(huán)境及使用方法
Python的虛擬環(huán)境極大地方便了人們的生活.本文介紹了虛擬環(huán)境的基礎(chǔ)知識(shí)以及使用方法,文中有非常詳細(xì)的說(shuō)明,需要的朋友可以參考下2021-06-06