赞
踩
新浪微博手机端地址:https://m.weibo.cn/
登录自己的微博账号。
右键-检查,刷新网页,Network,size排序,
其中Request_URL就是我们需要爬取微博的请求地址,如图:
User_Agent和Cookies:
譬如刘亦菲的这条微博:
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
程序比较粗糙,仅做新手练习。
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。