Python爬蟲爬取新浪微博內(nèi)容示例【基于代理IP】-創(chuàng)新互聯(lián)

本文實(shí)例講述了Python爬蟲爬取新浪微博內(nèi)容。分享給大家供大家參考,具體如下:

成都創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),蘆淞企業(yè)網(wǎng)站建設(shè),蘆淞品牌網(wǎng)站建設(shè),網(wǎng)站定制,蘆淞網(wǎng)站建設(shè)報(bào)價,網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,蘆淞網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。

用Python編寫爬蟲,爬取微博大V的微博內(nèi)容,本文以女神的微博為例(爬新浪m站:https://m.weibo.cn/u/1259110474)

一般做爬蟲爬取網(wǎng)站,選的都是m站,其次是wap站,最后考慮PC站。當(dāng)然,這不是絕對的,有的時候PC站的信息最全,而你又恰好需要全部的信息,那么PC站是你的選。一般m站都以m開頭后接域名, 所以本文開搞的網(wǎng)址就是 m.weibo.cn。

前期準(zhǔn)備

1.代理IP

網(wǎng)上有很多免費(fèi)代理ip,如西刺免費(fèi)代理IPhttp://www.xicidaili.com/,自己可找一個可以使用的進(jìn)行測試;

2.抓包分析

通過抓包獲取微博內(nèi)容地址,這里不再細(xì)說,不明白的小伙伴可以自行百度查找相關(guān)資料,下面直接上完整的代碼

完整代碼:

# -*- coding: utf-8 -*-
import urllib.request
import json
#定義要爬取的微博大V的微博ID
id='1259110474'
#設(shè)置代理IP
proxy_addr="122.241.72.191:808"
#定義頁面打開函數(shù)
def use_proxy(url,proxy_addr):
  req=urllib.request.Request(url)
  req.add_header("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0")
  proxy=urllib.request.ProxyHandler({'http':proxy_addr})
  opener=urllib.request.build_opener(proxy,urllib.request.HTTPHandler)
  urllib.request.install_opener(opener)
  data=urllib.request.urlopen(req).read().decode('utf-8','ignore')
  return data
#獲取微博主頁的containerid,爬取微博內(nèi)容時需要此id
def get_containerid(url):
  data=use_proxy(url,proxy_addr)
  content=json.loads(data).get('data')
  for data in content.get('tabsInfo').get('tabs'):
    if(data.get('tab_type')=='weibo'):
      containerid=data.get('containerid')
  return containerid
#獲取微博大V賬號的用戶基本信息,如:微博昵稱、微博地址、微博頭像、關(guān)注人數(shù)、粉絲數(shù)、性別、等級等
def get_userInfo(id):
  url='https://m.weibo.cn/api/container/getIndex?type=uid&value='+id
  data=use_proxy(url,proxy_addr)
  content=json.loads(data).get('data')
  profile_image_url=content.get('userInfo').get('profile_image_url')
  description=content.get('userInfo').get('description')
  profile_url=content.get('userInfo').get('profile_url')
  verified=content.get('userInfo').get('verified')
  guanzhu=content.get('userInfo').get('follow_count')
  name=content.get('userInfo').get('screen_name')
  fensi=content.get('userInfo').get('followers_count')
  gender=content.get('userInfo').get('gender')
  urank=content.get('userInfo').get('urank')
  print("微博昵稱:"+name+"\n"+"微博主頁地址:"+profile_url+"\n"+"微博頭像地址:"+profile_image_url+"\n"+"是否認(rèn)證:"+str(verified)+"\n"+"微博說明:"+description+"\n"+"關(guān)注人數(shù):"+str(guanzhu)+"\n"+"粉絲數(shù):"+str(fensi)+"\n"+"性別:"+gender+"\n"+"微博等級:"+str(urank)+"\n")
#獲取微博內(nèi)容信息,并保存到文本中,內(nèi)容包括:每條微博的內(nèi)容、微博詳情頁面地址、點(diǎn)贊數(shù)、評論數(shù)、轉(zhuǎn)發(fā)數(shù)等
def get_weibo(id,file):
  i=1
  while True:
    url='https://m.weibo.cn/api/container/getIndex?type=uid&value='+id
    weibo_url='https://m.weibo.cn/api/container/getIndex?type=uid&value='+id+'&containerid='+get_containerid(url)+'&page='+str(i)
    try:
      data=use_proxy(weibo_url,proxy_addr)
      content=json.loads(data).get('data')
      cards=content.get('cards')
      if(len(cards)>0):
        for j in range(len(cards)):
          print("-----正在爬取第"+str(i)+"頁,第"+str(j)+"條微博------")
          card_type=cards[j].get('card_type')
          if(card_type==9):
            mblog=cards[j].get('mblog')
            attitudes_count=mblog.get('attitudes_count')
            comments_count=mblog.get('comments_count')
            created_at=mblog.get('created_at')
            reposts_count=mblog.get('reposts_count')
            scheme=cards[j].get('scheme')
            text=mblog.get('text')
            with open(file,'a',encoding='utf-8') as fh:
              fh.write("----第"+str(i)+"頁,第"+str(j)+"條微博----"+"\n")
              fh.write("微博地址:"+str(scheme)+"\n"+"發(fā)布時間:"+str(created_at)+"\n"+"微博內(nèi)容:"+text+"\n"+"點(diǎn)贊數(shù):"+str(attitudes_count)+"\n"+"評論數(shù):"+str(comments_count)+"\n"+"轉(zhuǎn)發(fā)數(shù):"+str(reposts_count)+"\n")
        i+=1
      else:
        break
    except Exception as e:
      print(e)
      pass
if __name__=="__main__":
  file=id+".txt"
  get_userInfo(id)
  get_weibo(id,file)

當(dāng)前題目:Python爬蟲爬取新浪微博內(nèi)容示例【基于代理IP】-創(chuàng)新互聯(lián)
本文鏈接:http://muchs.cn/article30/dcjcpo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)、網(wǎng)站策劃、云服務(wù)器網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)虛擬主機(jī)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設(shè)