python+selenium爬取微博熱搜存入Mysql的實(shí)現(xiàn)方法
最終的效果
廢話不多少,直接上圖

這里可以清楚的看到,數(shù)據(jù)庫(kù)里包含了日期,內(nèi)容,和網(wǎng)站link
下面我們來(lái)分析怎么實(shí)現(xiàn)
使用的庫(kù)
import requests from selenium.webdriver import Chrome, ChromeOptions import time from sqlalchemy import create_engine import pandas as pd
目標(biāo)分析
這是微博熱搜的link:點(diǎn)我可以到目標(biāo)網(wǎng)頁(yè)

首先我們使用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ù)

我們看到,使用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, ChromeOptions
import time
from sqlalchemy import create_engine
import pandas as pd
def 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 pdf
def 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)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python3爬蟲學(xué)習(xí)之MySQL數(shù)據(jù)庫(kù)存儲(chǔ)爬取的信息詳解
- Python爬蟲爬取全球疫情數(shù)據(jù)并存儲(chǔ)到mysql數(shù)據(jù)庫(kù)的步驟
- Python爬取騰訊疫情實(shí)時(shí)數(shù)據(jù)并存儲(chǔ)到mysql數(shù)據(jù)庫(kù)的示例代碼
- Python如何爬取51cto數(shù)據(jù)并存入MySQL
- python 爬取古詩(shī)文存入mysql數(shù)據(jù)庫(kù)的方法
- python3爬取數(shù)據(jù)至mysql的方法
- Python爬取數(shù)據(jù)并寫入MySQL數(shù)據(jù)庫(kù)的實(shí)例
- Python3實(shí)現(xiàn)的爬蟲爬取數(shù)據(jù)并存入mysql數(shù)據(jù)庫(kù)操作示例
- python Selenium爬取內(nèi)容并存儲(chǔ)至MySQL數(shù)據(jù)庫(kù)的實(shí)現(xiàn)代碼
- Python爬取京東商品信息評(píng)論存并進(jìn)MySQL
相關(guān)文章
LRUCache的實(shí)現(xiàn)原理及利用python實(shí)現(xiàn)的方法
LruCache 是 Android 的一個(gè)內(nèi)部類,提供了基于內(nèi)存實(shí)現(xiàn)的緩存,而下面這篇文章主要給大家介紹了關(guān)于LRUCache的實(shí)現(xiàn)原理以及利用python實(shí)現(xiàn)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-11-11
基于Python輕松實(shí)現(xiàn)PDF轉(zhuǎn)圖片
PDF文件是我們?cè)谌粘9ぷ骱蛯W(xué)習(xí)中常用的文檔格式之一,但你知道嗎,你可以將PDF文件轉(zhuǎn)換為圖像,讓文檔變得更加生動(dòng)有趣,下面我們就來(lái)看看具體的實(shí)現(xiàn)方法吧2023-08-08
Python通過(guò)len函數(shù)返回對(duì)象長(zhǎng)度
這篇文章主要介紹了Python通過(guò)len函數(shù)返回對(duì)象長(zhǎng)度,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10
Python中zip()函數(shù)用法實(shí)例教程
這篇文章主要介紹了Python中zip()函數(shù)用法實(shí)例教程,對(duì)Python初學(xué)者有一定的借鑒價(jià)值,需要的朋友可以參考下2014-07-07
Python使用DrissionPage中ChromiumPage進(jìn)行自動(dòng)化網(wǎng)頁(yè)操作
DrissionPage 作為一款輕量級(jí)且功能強(qiáng)大的瀏覽器自動(dòng)化庫(kù),為開發(fā)者提供了豐富的功能支持,本文將使用DrissionPage中ChromiumPage進(jìn)行自動(dòng)化網(wǎng)頁(yè)操作,希望對(duì)大家有所幫助2025-03-03
在Python中使用循環(huán)進(jìn)行迭代的方法小結(jié)
Python中的循環(huán)結(jié)構(gòu)是編程中的重要組成部分,本文詳細(xì)介紹這兩種循環(huán)的使用方法、它們之間的差異以及如何選擇合適的循環(huán)類型,此外,我還將介紹一些高級(jí)循環(huán)控制技巧,如列表推導(dǎo)式和生成器表達(dá)式,感興趣的朋友一起看看吧2024-01-01

