国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

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

python+selenium爬取微博熱搜存入Mysql的實(shí)現(xiàn)方法

瀏覽:5日期:2022-06-29 08:20:33

最終的效果

廢話不多少,直接上圖

python+selenium爬取微博熱搜存入Mysql的實(shí)現(xiàn)方法

這里可以清楚的看到,數(shù)據(jù)庫(kù)里包含了日期,內(nèi)容,和網(wǎng)站link下面我們來(lái)分析怎么實(shí)現(xiàn)

使用的庫(kù)

import requestsfrom selenium.webdriver import Chrome, ChromeOptionsimport timefrom sqlalchemy import create_engineimport pandas as pd

目標(biāo)分析

這是微博熱搜的link:點(diǎn)我可以到目標(biāo)網(wǎng)頁(yè)

python+selenium爬取微博熱搜存入Mysql的實(shí)現(xiàn)方法

首先我們使用selenium對(duì)目標(biāo)網(wǎng)頁(yè)進(jìn)行請(qǐng)求然后我們使用xpath對(duì)網(wǎng)頁(yè)元素進(jìn)行定位,遍歷獲得所有數(shù)據(jù)然后使用pandas生成一個(gè)Dataframe對(duì)像,直接存入數(shù)據(jù)庫(kù)

一:得到數(shù)據(jù)

python+selenium爬取微博熱搜存入Mysql的實(shí)現(xiàn)方法

我們看到,使用xpath可以得到51條數(shù)據(jù),這就是各熱搜,從中我們可以拿到鏈接和標(biāo)題內(nèi)容

all = browser.find_elements_by_xpath(’//*[@id='pl_top_realtimehot']/table/tbody/tr/td[2]/a’) #得到所有數(shù)據(jù)context = [i.text for i in c] # 得到標(biāo)題內(nèi)容 links = [i.get_attribute(’href’) for i in c] # 得到link

然后我們?cè)偈褂脄ip函數(shù),將date,context,links合并zip函數(shù)是將幾個(gè)列表合成一個(gè)列表,并且按index對(duì)分列表的數(shù)據(jù)合并成一個(gè)元組,這個(gè)可以生產(chǎn)pandas對(duì)象。

dc = zip(dates, context, links) pdf = pd.DataFrame(dc, columns=[’date’, ’hotsearch’, ’link’])

其中date可以使用time模塊獲得

二:鏈接數(shù)據(jù)庫(kù)

這個(gè)很容易

enging = create_engine('mysql+pymysql://root:123456@localhost:3306/webo?charset=utf8')pdf.to_sql(name=’infromation’, con=enging, if_exists='append')

總代碼

from selenium.webdriver import Chrome, ChromeOptionsimport timefrom sqlalchemy import create_engineimport pandas as pddef get_data(): url = r'https://s.weibo.com/top/summary' # 微博的地址 option = ChromeOptions() option.add_argument(’--headless’) option.add_argument('--no-sandbox') browser = Chrome(options=option) browser.get(url) all = browser.find_elements_by_xpath(’//*[@id='pl_top_realtimehot']/table/tbody/tr/td[2]/a’) context = [i.text for i in all] links = [i.get_attribute(’href’) for i in all] date = time.strftime('%Y-%m-%d-%H_%M_%S', time.localtime()) dates = [] for i in range(len(context)): dates.append(date) # print(len(dates),len(context),dates,context) dc = zip(dates, context, links) pdf = pd.DataFrame(dc, columns=[’date’, ’hotsearch’, ’link’]) # pdf.to_sql(name=in, con=enging, if_exists='append') return pdfdef w_mysql(pdf): try: enging = create_engine('mysql+pymysql://root:123456@localhost:3306/webo?charset=utf8') pdf.to_sql(name=’infromation’, con=enging, if_exists='append') except: print(’出錯(cuò)了’)if __name__ == ’__main__’: xx = get_data() w_mysql(xx)

到此這篇關(guān)于python+selenium爬取微博熱搜存入Mysql的實(shí)現(xiàn)方法的文章就介紹到這了,更多相關(guān)python selenium爬取微博熱搜存入Mysql內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: 微博 Python
相關(guān)文章:
主站蜘蛛池模板: 宣城市| 普洱| 厦门市| 崇信县| 玉溪市| 睢宁县| 南汇区| 盐山县| 永嘉县| 营口市| 新竹市| 芒康县| 抚远县| 改则县| 巧家县| 潢川县| 扎兰屯市| 白沙| 赫章县| 阜城县| 武穴市| 六枝特区| 西青区| 阳东县| 高雄县| 彰化市| 鸡东县| 通化市| 武穴市| 扎赉特旗| 库伦旗| 五寨县| 南岸区| 延边| 永登县| 思南县| 司法| 科技| 盐城市| 桂平市| 大同县|