国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術(shù)文章
文章詳情頁

Python selenium爬取微博數(shù)據(jù)代碼實(shí)例

瀏覽:82日期:2022-07-24 17:56:03

爬取某人的微博數(shù)據(jù),把某人所有時(shí)間段的微博數(shù)據(jù)都爬下來。

具體思路:

創(chuàng)建driver-----get網(wǎng)頁----找到并提取信息-----保存csv----翻頁----get網(wǎng)頁(開始循環(huán))----...----沒有“下一頁”就結(jié)束,

用了while True,沒用自我調(diào)用函數(shù)

嘟大海的微博:https://weibo.com/u/1623915527

辦公室小野的微博:https://weibo.com/bgsxy

代碼如下

from selenium import webdriverfrom selenium.webdriver.common.keys import Keysimport csvimport osimport time #只有這2個(gè)參數(shù)設(shè)置,想爬誰的微博數(shù)據(jù)就在這里改地址和目標(biāo)csv名稱就行weibo_url = ’https://weibo.com/bgsxy?profile_ftype=1&is_all=1#_0’csv_name = ’bgsxy_allweibo.csv’ def start_chrome(): print(’開始創(chuàng)建瀏覽器’) driver = webdriver.Chrome(executable_path=’C:/Users/lori/Desktop/python52project/chromedriver_win32/chromedriver.exe’) driver.start_client() return driver def get_web(url): #獲取網(wǎng)頁,并下拉到最底部 print(’開始打開指定網(wǎng)頁’) driver.get(url) time.sleep(7) scoll_down() time.sleep(5) def scoll_down(): # 滾輪下拉到最底部 html_page = driver.find_element_by_tag_name(’html’) for i in range(7): print(i) html_page.send_keys(Keys.END) time.sleep(1) def get_data(): print(’開始查找并提取數(shù)據(jù)’) card_sel = ’div.WB_cardwrap.WB_feed_type’ time_sel = ’a.S_txt2[node-type='feed_list_item_date']’ source_sel = ’a.S_txt2[suda-uatrack='key=profile_feed&value=pubfrom_guest']’ content_sel = ’div.WB_text.W_f14’ interact_sel = ’span.line.S_line1>span>em:nth-child(2)’ cards = driver.find_elements_by_css_selector(card_sel) info_list = [] for card in cards: time = card.find_elements_by_css_selector(time_sel)[0].text #雖然有可能在一個(gè)card中有2個(gè)time元素,我們?nèi)〉谝粋€(gè)就對(duì) if card.find_elements_by_css_selector(source_sel): source = card.find_elements_by_css_selector(source_sel)[0].text else: source = ’’ content = card.find_elements_by_css_selector(content_sel)[0].text link = card.find_elements_by_css_selector(time_sel)[0].get_attribute(’href’) trans = card.find_elements_by_css_selector(interact_sel)[1].text comment = card.find_elements_by_css_selector(interact_sel)[2].text like = card.find_elements_by_css_selector(interact_sel)[3].text info_list.append([time,source,content,link,trans,comment,like]) return info_list def save_csv(info_list,csv_name): csv_path = ’./’ + csv_name print(’開始寫入csv文件’) if os.path.exists(csv_path): with open(csv_path,’a’,newline=’’,encoding=’utf-8-sig’) as f: #newline=’’避免空行;encoding=’utf-8-sig’比utf8牛,保存中文沒問題 writer = csv.writer(f) writer.writerows(info_list) else: with open(csv_path,’w+’,newline=’’,encoding=’utf-8-sig’) as f: writer = csv.writer(f) writer.writerow([’發(fā)布時(shí)間’,’來源’,’內(nèi)容’,’鏈接’,’轉(zhuǎn)發(fā)數(shù)’,’評(píng)論數(shù)’,’點(diǎn)贊數(shù)’]) writer.writerows(info_list) time.sleep(5) def next_page_url(): next_page_sel = ’a.page.next’ next_page_ele = driver.find_elements_by_css_selector(next_page_sel) if next_page_ele: return next_page_ele[0].get_attribute(’href’) else: return None driver = start_chrome()input(’請(qǐng)?jiān)赾hrome中登錄weibo.com’) # 暫停程序,手動(dòng)登錄weibo.com while True: get_web(weibo_url) info_list = get_data() save_csv(info_list,csv_name) if next_page_url(): weibo_url = next_page_url() else: print(’爬取結(jié)束’) break

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: 微博 Python
相關(guān)文章:
主站蜘蛛池模板: 国产精品亚洲精品不卡 | 男人的天堂欧美精品色偷偷 | 亚洲美女精品视频 | 中国女人毛茸茸免费视频 | 欧美日韩亚洲高清不卡一区二区三区 | 欧美日韩视频免费播放 | 男人的天堂官网 | 欧美日韩精品一区二区三区不卡 | 欧美日韩成人 | 免费看一级欧美激情毛片 | 久久精品网站免费观看调教 | 综合亚洲精品一区二区三区 | 草草草在线观看 | 日本成人在线看 | 国产的一级片 | 九九九九视频 | 伊在人亚洲香蕉精品区 | 午夜亚洲精品 | 国产免费播放一区二区 | 最近韩国日本免费免费版 | 三级黄色免费看 | 92精品国产成人观看免费 | 深夜爽爽福利gif在线观看 | 久久99中文字幕 | 目韩一区二区三区系列片丶 | 国产一区二区在线看 | 国产免费久久精品 | 欧美高清在线视频一区二区 | 久久aⅴ免费观看 | 97在线免费 | 精品国产90后在线观看 | 中文字幕99在线精品视频免费看 | 国内精品久久久久久影院8f | 久久在线国产 | 国内精品1区1区3区4区 | 日本精品视频一区二区三区 | 久草免费看 | 视频一区 在线 | 国产自在自线午夜精品视频在 | 久久成人精品视频 | 一区二区三区国产 |