python+selenium爬取微博熱搜存入Mysql的實現(xiàn)方法
最終的效果
廢話不多少,直接上圖
這里可以清楚的看到,數(shù)據(jù)庫里包含了日期,內(nèi)容,和網(wǎng)站link
下面我們來分析怎么實現(xiàn)
使用的庫
import requests from selenium.webdriver import Chrome, ChromeOptions import time from sqlalchemy import create_engine import pandas as pd
目標分析
這是微博熱搜的link:點我可以到目標網(wǎng)頁
首先我們使用selenium對目標網(wǎng)頁進行請求
然后我們使用xpath對網(wǎng)頁元素進行定位,遍歷獲得所有數(shù)據(jù)
然后使用pandas生成一個Dataframe對像,直接存入數(shù)據(jù)庫
一:得到數(shù)據(jù)
我們看到,使用xpath可以得到51條數(shù)據(jù),這就是各熱搜,從中我們可以拿到鏈接和標題內(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] # 得到標題內(nèi)容 links = [i.get_attribute('href') for i in c] # 得到link
然后我們再使用zip函數(shù),將date,context,links合并
zip函數(shù)是將幾個列表合成一個列表,并且按index對分列表的數(shù)據(jù)合并成一個元組,這個可以生產(chǎn)pandas對象。
dc = zip(dates, context, links) pdf = pd.DataFrame(dc, columns=['date', 'hotsearch', 'link'])
其中date可以使用time模塊獲得
二:鏈接數(shù)據(jù)庫
這個很容易
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('出錯了') if __name__ == '__main__': xx = get_data() w_mysql(xx)
到此這篇關(guān)于python+selenium爬取微博熱搜存入Mysql的實現(xiàn)方法的文章就介紹到這了,更多相關(guān)python selenium爬取微博熱搜存入Mysql內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python3爬蟲學(xué)習(xí)之MySQL數(shù)據(jù)庫存儲爬取的信息詳解
- Python爬蟲爬取全球疫情數(shù)據(jù)并存儲到mysql數(shù)據(jù)庫的步驟
- Python爬取騰訊疫情實時數(shù)據(jù)并存儲到mysql數(shù)據(jù)庫的示例代碼
- Python如何爬取51cto數(shù)據(jù)并存入MySQL
- python 爬取古詩文存入mysql數(shù)據(jù)庫的方法
- python3爬取數(shù)據(jù)至mysql的方法
- Python爬取數(shù)據(jù)并寫入MySQL數(shù)據(jù)庫的實例
- Python3實現(xiàn)的爬蟲爬取數(shù)據(jù)并存入mysql數(shù)據(jù)庫操作示例
- python Selenium爬取內(nèi)容并存儲至MySQL數(shù)據(jù)庫的實現(xiàn)代碼
- Python爬取京東商品信息評論存并進MySQL
相關(guān)文章
Python數(shù)據(jù)存儲之XML文檔和字典的互轉(zhuǎn)
這篇文章主要介紹了Python數(shù)據(jù)存儲之XML文檔和字典的互轉(zhuǎn),通過如何將一個字典轉(zhuǎn)換為XML文檔,并將該XML文檔保存為文本文件的提問展開主題相關(guān)介紹,需要的朋友可以參考一下下面文章內(nèi)容2022-06-06python飛機大戰(zhàn)pygame碰撞檢測實現(xiàn)方法分析
這篇文章主要介紹了python飛機大戰(zhàn)pygame碰撞檢測實現(xiàn)方法,結(jié)合實例形式分析了Python使用pygame實現(xiàn)飛機大戰(zhàn)游戲中碰撞檢測的原理與相關(guān)操作技巧,需要的朋友可以參考下2019-12-12python 基礎(chǔ)學(xué)習(xí)第二彈 類屬性和實例屬性
本人c程序員,最近開始學(xué)python,深深的被python的強大所吸引,今后也會把學(xué)到的點點滴滴記錄下來,現(xiàn)在分享一下關(guān)于類屬性和實例屬性的一些問題,很基礎(chǔ)的東西2012-08-08