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)文章
python圖形繪制奧運(yùn)五環(huán)實(shí)例講解
在本文里我們給大家整理了一篇關(guān)于python圖形繪制奧運(yùn)五環(huán)的實(shí)例內(nèi)容,大家可以跟著學(xué)習(xí)下。2019-09-09
Python數(shù)據(jù)存儲(chǔ)之XML文檔和字典的互轉(zhuǎn)
這篇文章主要介紹了Python數(shù)據(jù)存儲(chǔ)之XML文檔和字典的互轉(zhuǎn),通過(guò)如何將一個(gè)字典轉(zhuǎn)換為XML文檔,并將該XML文檔保存為文本文件的提問(wèn)展開主題相關(guān)介紹,需要的朋友可以參考一下下面文章內(nèi)容2022-06-06
python飛機(jī)大戰(zhàn)pygame碰撞檢測(cè)實(shí)現(xiàn)方法分析
這篇文章主要介紹了python飛機(jī)大戰(zhàn)pygame碰撞檢測(cè)實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Python使用pygame實(shí)現(xiàn)飛機(jī)大戰(zhàn)游戲中碰撞檢測(cè)的原理與相關(guān)操作技巧,需要的朋友可以參考下2019-12-12
Django使用echarts進(jìn)行可視化展示的實(shí)踐
可視化是將數(shù)據(jù)轉(zhuǎn)換成圖形或圖像在屏幕上顯示出來(lái),本文主要介紹了Django使用echarts進(jìn)行可視化展示的實(shí)踐,感興趣的可以了解一下2021-06-06
python 基礎(chǔ)學(xué)習(xí)第二彈 類屬性和實(shí)例屬性
本人c程序員,最近開始學(xué)python,深深的被python的強(qiáng)大所吸引,今后也會(huì)把學(xué)到的點(diǎn)點(diǎn)滴滴記錄下來(lái),現(xiàn)在分享一下關(guān)于類屬性和實(shí)例屬性的一些問(wèn)題,很基礎(chǔ)的東西2012-08-08

