当前位置:   article > 正文

Python爬取新浪微博实操_爬虫能看到私密微博吗

爬虫能看到私密微博吗

第一步:选择从手机端爬取

新浪微博手机端地址:https://m.weibo.cn/
登录自己的微博账号。

第二步:爬取刘亦菲的微博为例:

2.1获取需要爬取微博的Request_URL,以及构造网络请求的User_Agent和Cookies:

右键-检查,刷新网页,Network,size排序,
其中Request_URL就是我们需要爬取微博的请求地址,如图:
在这里插入图片描述
User_Agent和Cookies:
在这里插入图片描述

2.2点击Preview,微博的信息存储在card_type=9的列表中,

譬如刘亦菲的这条微博:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

第三步:具体的代码如下:

import requests
from bs4 import BeautifulSoup
import json
import re
import time

#1:使用正则表达之去除html标签
def filter_tags(htmlstr):
#先过滤CDATA
    re_cdata=re.compile('//<!\[CDATA\[[^>]*//\]\]>',re.I) #匹配CDATA
    re_script=re.compile('<\s*script[^>]*>[^<]*<\s*/\s*script\s*>',re.I)#Script
    re_style=re.compile('<\s*style[^>]*>[^<]*<\s*/\s*style\s*>',re.I)#style
    re_br=re.compile('<br\s*?/?>')#处理换行
    re_h=re.compile('</?\w+[^>]*>')#HTML标签
    re_comment=re.compile('<!--[^>]*-->')#HTML注释
    s=re_cdata.sub('',htmlstr)#去掉CDATA
    s=re_script.sub('',s) #去掉SCRIPT
    s=re_style.sub('',s)#去掉style
    s=re_br.sub('\n',s)#将br转换为换行
    s=re_h.sub('',s) #去掉HTML 标签
    s=re_comment.sub('',s)#去掉HTML注释
    s=s.replace('网页链接','')
    return s


headers = {'user-agent':
          '自行补充'
          }
          
cookies = {'cookies':
           '自行补充'
          }

url_1 = input ( "请输入微博博主的url:" + str() ) 


i = 1
count = 1
while True:
    url = url_1 + '&page=' + str(i)
    req = requests.get(url, headers=headers, cookies=cookies)
    text = req.text
    try:
        content = json.loads(text).get('data')
        cards = content.get('cards')  
        if (len(cards)>0):
            for j in range(len(cards)):
                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')
                    text1=mblog.get('text')
                    text2=filter_tags(text1)
                    print ( str(count) + "\n"+"发布时间:"+str(created_at)+"\n"+"微博内容:"+text2+"\n")
                    count = count + 1
            i = i + 1
            time.sleep(5)
        else:
            break 
    except Exception as e:
        print (e)
        pass    

  • 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

第四:执行上段代码:

在这里插入图片描述
程序比较粗糙,仅做新手练习。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/blog/article/detail/57060
推荐阅读
相关标签
  

闽ICP备14008679号