国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - Python爬蟲爬取新浪微博內容示例【基于代理IP】

Python爬蟲爬取新浪微博內容示例【基于代理IP】

2021-03-25 09:58Jepson2017 Python

這篇文章主要介紹了Python爬蟲爬取新浪微博內容,結合實例形式分析了Python基于代理IP實現的微博爬取與抓包分析相關操作技巧,需要的朋友可以參考下

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

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

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

前期準備

1.代理IP

網上有很多免費代理ip,如西刺免費代理IPhttp://www.xicidaili.com/,自己可找一個可以使用的進行測試;

2.抓包分析

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

完整代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# -*- coding: utf-8 -*-
import urllib.request
import json
#定義要爬取的微博大V的微博ID
id='1259110474'
#設置代理IP
proxy_addr="122.241.72.191:808"
#定義頁面打開函數
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,爬取微博內容時需要此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賬號的用戶基本信息,如:微博昵稱、微博地址、微博頭像、關注人數、粉絲數、性別、等級等
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"+"是否認證:"+str(verified)+"\n"+"微博說明:"+description+"\n"+"關注人數:"+str(guanzhu)+"\n"+"粉絲數:"+str(fensi)+"\n"+"性別:"+gender+"\n"+"微博等級:"+str(urank)+"\n")
#獲取微博內容信息,并保存到文本中,內容包括:每條微博的內容、微博詳情頁面地址、點贊數、評論數、轉發數等
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"+"發布時間:"+str(created_at)+"\n"+"微博內容:"+text+"\n"+"點贊數:"+str(attitudes_count)+"\n"+"評論數:"+str(comments_count)+"\n"+"轉發數:"+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)

爬取結果

Python爬蟲爬取新浪微博內容示例【基于代理IP】

Python爬蟲爬取新浪微博內容示例【基于代理IP】

希望本文所述對大家Python程序設計有所幫助。

原文鏈接:https://blog.csdn.net/d1240673769/article/details/74278547

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 干片网 | 国产精品久久久久久久久久久免费看 | 中文久久久久久 | 中文字幕在线视频一区 | 成人亚洲网 | 日韩中文字幕一区二区 | 日本色综合 | 九色 在线 | 亚洲在线电影 | 一区二区三区精品 | 国产欧美一区二区精品性色 | yellow在线视频免费观看 | 人人爱人人爽 | 久久久久国产一区二区三区四区 | 激情久久婷婷 | www久久久| 中文字幕一区二区三区四区不卡 | 国产日韩欧美在线 | 国产免费激情视频 | 精品久久久久国产 | 久久亚洲二区 | 成人h免费观看视频 | 午夜激情影院 | 国产一区二区在线播放 | www.久 | 亚洲少妇视频 | 91性高湖久久久久久久久网站 | 成人精品视频一区二区三区 | 九九热欧美 | 日韩免费一区 | 欧美 日韩 精品 | www.久久99 | 在线小视频国产 | 日韩精品免费一区二区夜夜嗨 | 国产日韩精品一区二区 | 日韩精品极品视频在线观看免费 | 欧美日韩中文 | 中文字幕精品一区二区精品 | 欧美在线亚洲 | 亚洲一区二区三区高清 | 亚洲视频免费观看 |