欧美亚洲中文,在线国自产视频,欧洲一区在线观看视频,亚洲综合中文字幕在线观看

      1. <dfn id="rfwes"></dfn>
          <object id="rfwes"></object>
        1. 站長資訊網(wǎng)
          最全最豐富的資訊網(wǎng)站

          python實現(xiàn)爬取微博熱搜存入Mysql

          python實現(xiàn)爬取微博熱搜存入Mysql

          免費學習推薦:python視頻教程

          python爬取微博熱搜存入Mysql

            • 最終的效果
            • 使用的庫
            • 目標分析
            • 一:得到數(shù)據(jù)
            • 二:鏈接數(shù)據(jù)庫
            • 總代碼

          最終的效果

          廢話不多少,直接上圖
          python實現(xiàn)爬取微博熱搜存入Mysql
          這里可以清楚的看到,數(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)頁
          python實現(xiàn)爬取微博熱搜存入Mysql
          首先我們使用selenium對目標網(wǎng)頁進行請求
          然后我們使用xpath對網(wǎng)頁元素進行定位,遍歷獲得所有數(shù)據(jù)
          然后使用pandas生成一個Dataframe對像,直接存入數(shù)據(jù)庫

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

          python實現(xiàn)爬取微博熱搜存入Mysql
          我們看到,使用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)

          希望能夠幫到大家一點,大家一起共同進步,共同成長!
          祝大家新年快樂?。?!

          相關免費學習推薦:python教程(視頻)

          贊(0)
          分享到: 更多 (0)
          網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號