亚洲最大看欧美片,亚洲图揄拍自拍另类图片,欧美精品v国产精品v呦,日本在线精品视频免费

  • 站長(zhǎng)資訊網(wǎng)
    最全最豐富的資訊網(wǎng)站

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

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

    免費(fèi)學(xué)習(xí)推薦:python視頻教程

    python爬取微博熱搜存入Mysql

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

    最終的效果

    廢話不多少,直接上圖
    python實(shí)現(xiàn)爬取微博熱搜存入Mysql
    這里可以清楚的看到,數(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è)
    python實(shí)現(xiàn)爬取微博熱搜存入Mysql
    首先我們使用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實(shí)現(xiàn)爬取微博熱搜存入Mysql
    我們看到,使用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)

    希望能夠幫到大家一點(diǎn),大家一起共同進(jìn)步,共同成長(zhǎng)!
    祝大家新年快樂(lè)?。?!

    相關(guān)免費(fèi)學(xué)習(xí)推薦:python教程(視頻)

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