資源簡介
這是新浪微博爬蟲,采用python+selenium實現。
免費資源,希望對你有所幫助,雖然是傻瓜式爬蟲,但是至少能運行。同時rar中包括源碼及爬取的示例。
參考我的文章:
http://blog.csdn.net/eastmount/article/details/50720436
[python爬蟲] Selenium爬取新浪微博內容及用戶信息
http://blog.csdn.net/eastmount/article/details/51231852
[Python爬蟲] Selenium爬取新浪微博客戶端用戶信息、熱點話題及評論 (上)
主要爬取內容包括:
新浪微博手機端用戶信息和微博信息。
用戶信息:包括用戶ID、用戶名、微博數、粉絲數、關注數等。
微博信息:包括轉發或原創、點贊數、轉發數、評論數、發布時間、微博內容等。
安裝過程:
1.先安裝Python環境,作者是Python 2.7.8
2.再安裝PIP或者easy_install
3.通過命令pip install selenium安裝selenium,它是自動測試、爬蟲的工具
4.然后修改代碼中的用戶名和密碼,填寫你自己的用戶名和密碼
5.運行程序,自動調用Firefox瀏覽器登陸微博
注意:手機端信息更加精致簡單,而且沒有動態加載的一些限制,但是如微博或粉絲id只顯示20頁,這是它的缺點;
而客戶端可能存在動態加載,如評論和微博,但是它的信息更加完整。
[源碼] 爬取移動端微博信息
spider_selenium_sina_content.py
輸入:
明星用戶id列表,采用URL+用戶id進行訪問(這些id可以從一個用戶的關注列表里面獲?。?SinaWeibo_List_best_1.txt
輸出:
微博信息及用戶基本信息
SinaWeibo_Info_best_1.txt
Megry_Result_Best.py
該文件用戶整理某一天的用戶微博信息,如2016年4月23日
[源碼] 爬取客戶端微博信息
爬取客戶端信息,但是評論是動態加載,還在研究中
weibo_spider2.py
By:Eastmount 2016-04-24

代碼片段和文件信息
#?coding=utf-8
“““??
Created?on?2016-04-24?@author:?Eastmount
功能:?爬取新浪微博用戶的信息及微博評論
網址:http://weibo.cn/?數據量更小?相對http://weibo.com/
“““????
import?time????????????
import?re????????????
import?os????
import?sys??
import?codecs??
import?shutil
import?urllib?
from?selenium?import?webdriver????????
from?selenium.webdriver.common.keys?import?Keys????????
import?selenium.webdriver.support.ui?as?ui????????
from?selenium.webdriver.common.action_chains?import?ActionChains
#先調用無界面瀏覽器PhantomJS或Firefox????
#driver?=?webdriver.PhantomJS(executable_path=“G:\phantomjs-1.9.1-windows\phantomjs.exe“)????
driver?=?webdriver.Firefox()
wait?=?ui.WebDriverWait(driver10)
#全局變量?文件操作讀寫信息
inforead?=?codecs.open(“SinaWeibo_List_best_1.txt“?‘r‘?‘utf-8‘)
infofile?=?codecs.open(“SinaWeibo_Info_best_1.txt“?‘a‘?‘utf-8‘)
#********************************************************************************
#????????????????????????????第一步:?登陸weibo.cn?
#????????該方法針對weibo.cn有效(明文形式傳輸數據)?weibo.com見學弟設置POST和Header方法
#????????????????LoginWeibo(username?password)?參數用戶名?密碼
#********************************************************************************
def?LoginWeibo(username?password):
????try:
????????#輸入用戶名/密碼登錄
????????print?u‘準備登陸Weibo.cn網站...‘
????????driver.get(“http://login.sina.com.cn/“)
????????elem_user?=?driver.find_element_by_name(“username“)
????????elem_user.send_keys(username)?#用戶名
????????elem_pwd?=?driver.find_element_by_name(“password“)
????????elem_pwd.send_keys(password)??#密碼
????????#elem_rem?=?driver.find_element_by_name(“safe_login“)
????????#elem_rem.click()?????????????#安全登錄
????????#重點:?暫停時間輸入驗證碼(http://login.weibo.cn/login/?手機端需要)
????????time.sleep(20)
????????
????????#elem_sub?=?driver.find_element_by_xpath(“//input[@class=‘smb_btn‘]“)
????????#elem_sub.click()??????????????#點擊登陸?因無name屬性
????????elem_pwd.send_keys(Keys.RETURN)
????????time.sleep(2)
????????
????????#獲取Coockie?推薦資料:http://www.cnblogs.com/fnng/p/3269450.html
????????print?driver.current_url
????????print?driver.get_cookies()??#獲得cookie信息?dict存儲
????????print?u‘輸出Cookie鍵值對信息:‘
????????for?cookie?in?driver.get_cookies():?
????????????#print?cookie
????????????for?key?in?cookie:
????????????????print?key?cookie[key]
????????????????????
????????#driver.get_cookies()類型list?僅包含一個元素cookie類型dict
????????print?u‘登陸成功...‘
????????
????????
????except?Exceptione:??????
????????print?“Error:?“e
????finally:????
????????print?u‘End?LoginWeibo!\n\n‘
#********************************************************************************
#??????????????????第二步:?訪問個人頁面http://weibo.cn/5824697471并獲取信息
#????????????????????????????????VisitPersonPage()
#????????編碼常見錯誤?UnicodeEncodeError:?‘ascii‘?codec?can‘t?encode?characters?
#********************************************************************************
def?VisitPersonPage(user_id):
????try:
????????global?infofile???????#全局文件變量
????????url?=?“http:/
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????5628??2016-04-24?20:31??新浪微博爬蟲\[源碼]?爬取客戶端微博信息\SinaWeibo_Info_best_1.txt
?????文件?????????27??2016-04-24?03:45??新浪微博爬蟲\[源碼]?爬取客戶端微博信息\SinaWeibo_List_best_1.txt
?????文件???????8119??2016-04-24?20:31??新浪微博爬蟲\[源碼]?爬取客戶端微博信息\weibo_spider2.py
?????文件??????17680??2016-04-24?21:18??新浪微博爬蟲\[源碼]?爬取移動端個人信息?關注id和粉絲id?(速度慢)\SinaWeibo_Info_1.txt
?????文件?????????50??2016-04-24?21:17??新浪微博爬蟲\[源碼]?爬取移動端個人信息?關注id和粉絲id?(速度慢)\SinaWeibo_List_1.txt
?????文件??????14884??2016-04-24?21:19??新浪微博爬蟲\[源碼]?爬取移動端個人信息?關注id和粉絲id?(速度慢)\spider_selenium_sina_info_other_userid_all.py
?????文件??????13386??2016-04-24?20:55??新浪微博爬蟲\[源碼]?爬取移動端微博信息?(強推)\2016-04-23\20160423_SinaWeibo_Num_Best.txt
?????文件???????1595??2016-04-24?20:55??新浪微博爬蟲\[源碼]?爬取移動端微博信息?(強推)\2016-04-23\Megry_Result_Best.py
?????文件?????237289??2016-04-24?20:52??新浪微博爬蟲\[源碼]?爬取移動端微博信息?(強推)\2016-04-23\SinaWeibo_Info_best_1.txt
?????文件????????189??2016-04-24?20:46??新浪微博爬蟲\[源碼]?爬取移動端微博信息?(強推)\2016-04-23\SinaWeibo_List_best_1.txt
?????文件??????12115??2016-04-24?20:54??新浪微博爬蟲\[源碼]?爬取移動端微博信息?(強推)\2016-04-23\spider_selenium_sina_content.py
?????文件????????840??2016-04-24?21:02??新浪微博爬蟲\運行配置過程.txt
?????目錄??????????0??2016-04-24?20:55??新浪微博爬蟲\[源碼]?爬取移動端微博信息?(強推)\2016-04-23
?????目錄??????????0??2016-04-24?20:42??新浪微博爬蟲\[源碼]?爬取客戶端微博信息
?????目錄??????????0??2016-04-24?21:18??新浪微博爬蟲\[源碼]?爬取移動端個人信息?關注id和粉絲id?(速度慢)
?????目錄??????????0??2016-04-24?20:46??新浪微博爬蟲\[源碼]?爬取移動端微博信息?(強推)
?????目錄??????????0??2016-04-24?21:18??新浪微博爬蟲
-----------?---------??----------?-----??----
???????????????311802????????????????????17
- 上一篇:python鳳凰新聞數據分析
- 下一篇:python坐標轉換程序
評論
共有 條評論