挑戰(zhàn)練習微博搜索爬取好課加微ypkc_第1頁
挑戰(zhàn)練習微博搜索爬取好課加微ypkc_第2頁
挑戰(zhàn)練習微博搜索爬取好課加微ypkc_第3頁
挑戰(zhàn)練習微博搜索爬取好課加微ypkc_第4頁
全文預覽已結(jié)束

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領

文檔簡介

1.如何登錄2.如何按條件高級搜索指定4.如何獲取每條中的信息6.如何到csv文件中1.利用webdriver首頁,人工實現(xiàn)登錄操作,然后等待頁面跳轉(zhuǎn)2.GETURLGET3.selenium“end”4.csscss5.查找頁面是否有“下一頁”按鈕,如果有則該地址,否則結(jié)束爬蟲6.利用csv庫,將文件為csv文件#coding:utf-8importtimeimportcsvimportosfromseleniumimportfromsele mon.keysimportKeys#CSV文件路徑CSV_PATHweibo_ad.csv'#登錄頁面URLLOGIN_URL= #開啟瀏覽器def#driver=webdriver.Chrome(executable_path='./chromedriver')driver=webdriver.Chrome()returndriver#生成GETURLdefquery(today): #滾動屏幕defscroll_down(driver):html_page=driver.find_element_by_tag_name('html')foriinrange(10):#獲取頁面信息def#cards_sel='#Pl_Official_MyProfileFeed21>div>div'cards_sel='#pl_weibo_direct>div>div.feed_lists.W_texta>div'cards=driver.find_elements_by_css_selector(cards_sel)info_list=forcardin ='div>div.WB_feed_detail.clearfix>dl>div>div.content.clearfix>div.feed_content.wbcon>a.W_texta.W_fb'content_sel='div>div.WB_feed_detail.clearfix>dl>div>div.content.clearfix>div.feed_content.wbcon>p' ='div>div.WB_feed_detail.clearfix>dl>div>div.content.clearfix>div.feed_from.W_textb>a.W_textb' ='div>div.WB_feed_detail.clearfix>dl>div>div.content.clearfix>div.feed_from.W_textb>a.W_textb' ='div>div.feed_action.clearfix>ul>li:nth-child(2)>a>span>em' ='div>div.feed_action.clearfix>ul>li:nth-child(3)>a>span>

='div>div.feed_action.clearfix>ul>li:nth-child(4)>a>span>em'name=card.find_element_by_css_selector(name_sel).textcontent=card.find_element_by_css_selector(content_sel).texttime=card.find_element_by_css_selector(time_sel).textlink=card.find_element_by_css_selector(link_sel).get_attribute('href')relay=card.find_element_by_css_selector(relay_sel).textcomm=card.find_element_by_css_selector(comm_sel).textlike=card.find_element_by_css_selector(like_sel).textinfo_list.append([name,content,time,link,relay,comm,like])returninfo_list#翻頁defnext_sel='#pl_weibo_direct>div>div.WB_cardwrap.S_bg2.relative>div>a'next_page=driver.find_elements_by_css_selector(next_sel)ifreturnnext_page[0].get_attribute('href')#保存數(shù)據(jù)到CSV文件defifutf-8#在打開csvutf-8,否則可能會顯示亂碼withopen(CSV_PATH,'a',encoding='utf-8')asf:writer=csv.writer(f)

withopen(CSV_PATH,'w+',encoding='utf-8')asf:writer=csv.writer(f)CSVwriter.writerow(['用戶名','內(nèi)容','時間','','轉(zhuǎn)發(fā)量','評論量',#登錄頁面deflogin(driver,url):#啟動爬蟲的函數(shù)defrun_crawler(driver,url):info=find_cards_info(driver)next_page=find_next(driver)ifnext_page:run_cr

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論