python 计算机程序设计语言

admin 3837 22
python 计算机程序设计语言

百度网盘课程

通用入口链接!10000G.课程都有!一起学习吧!

立即点击↓ 获取课程!

python 计算机程序设计语言

爬取新浪微博数据()

具体要感谢大神贡献的代码,大神链接

代码我做了些修改满足我的需求,以下为代码:

#-* - 8-*-

=' '=' ' #=' ' ' deliver _ proxy(URL,proxy _ addr): req=urlib。请求。请求。add _ header(' User-Agent ',' Mozilla/5.0(windows nt 6.1;WOW64)AppleWebKit/537.36(KHTML,喜欢壁虎)Chrome/49。0 .2623 .221 safari/537.36 se 2。XMETASR 1.0’)代理=URL lib。请求。代理处理程序({ ' http ' : proxy _ addr })open er=URL lib。请求。build _ open er(代理,URL lib。请求。httphandler)URL lib。请求。install _ open er(open er)data=URL lib。请求.读取().decode('utf-8 ',' ignore ')returndatadefget _ containerid(URL): data=use _ proxy(URL,proxy_addr)content=json.loads(数据).获取数据内容的数据。get(' Tabsinfo ').get(' tab '): if(数据。get(' tab _ type ')=' Weibo '): containerid=data。get(' containerid ')returncontaineridefget _ UserInfo(id): URL=' https://m。微博。cn/API/container/GetIndex?type=uid value=' iddata=use _ proxy(URL,proxy_addr)content=json.loads(数据).get(' data ')profile _ image _ URL=content。获取('用户信息').get(' profile _ image _ URL ')描述=内容。获取('用户信息').get(' description ')profile _ URL=content。获取('用户信息').获取(' profile _ URL ')已验证=内容。获取('用户信息').get(' verified ')关珠=内容。获取('用户信息').get(' follow _ count ')name=content。获取('用户信息').get(' screen _ name ')fensi=content。获取('用户信息').get(' followers _ count ')gender=content。获取('用户信息').get(' gender ')urank=content。获取('用户信息').get('urank ')打印('微博昵称:"名称"

\ ' \ '微博主页地址:' profile_url '

\ ' \ '微博头像地址:' profile_image_url '

\ ' \ '是否认证:" str(已验证)”

\ ' \ '微博说明:"描述"

\ ' \ '关注人数:' str(guanzhu)'

\ ' \ '粉丝数:' str(fensi)'

\ ' \ '性别:"性别"

\ ' \ '微博等级:' str(urank)

)#获取微博内容信息,并保存到文本中,内容包括:每条微博的内容、微博详情页面地址、点赞数、评论数、转发数等defget_weibo_store(id,file): I=1而etru : URl=' https://m。微博。cn/API/container/GetIndex?type=uid value=' id Weibo _ URL=' https://m。微博。cn/API/container/GetIndex?type=uid value=' \ id ' containerid=' get _ containerid(URL)' page=' str(I)try : data=use _ proxy(Weibo _ URL,proxy _ addr)print(data)content=JSON。负载(数据).get(' data ')卡片=内容。get(' cards ')if(len(cards)0): for Jin range(len(cards)): cards _ type=cards[j].get(' card _ type ')if(card _ type==9): blog=cards[j].get(' mblog ')attributes _ count=mblog。get(' attributes _ count ')comments _ count=mblog。get(' comments _ count ')created _ at=mblog。get(' created _ at ')remots _ count=mblog。get(' remots _ count ')方案=cards[j].get(' scheme ')text=mblog。以FH : FH打开(文件,' a ',编码=' utf-8 ')获取(' text ')。write('-第str(i)页,第str(j)'条微博-' '

)fh.write('微博地址:"字符串(方案)"

\ ' \ '发布时间:" str(created_at)"

\ ' \ '微博内容:"文本"

\ ' \ '点赞数:" str(态度_计数)”

\ ' \ '评论数:' str(comments_count)'

\ ' \ '转发数:“str(rest ts _ count)”

)client=MongoClient(MONGO _ HOST)# ConnectMongoDBDB=client。Weibo _ database # created db data _ JSON=JSON。从witter #加载(数据)# Decodethejson将数据插入Mongodbintoacollectiondb。chongbin _ collection。insert(data _ JSON)I=1 LSE : break exception ase : print(e)passif _ _ name _ _=' _ _ main _ _ ' : file=id。txt ' get _ UserInfo(id)get _ Weibo _ store(id,文件)

代码说明

python 计算机程序设计语言

代码运行后爬取的是微博上某个人的的数据,查看某人编号可以到那个人的主页然后查看网页源代码搜索oid,这个关键字就是身份证。然后会爬取这个人的所有微博数据,代码会输出数据同时到mongodb数据库和文档。

他的这种爬取方法设置匿名代理而不需要申请微博的API

配置需求和错误说明:

下面说一下配置需求,我用的是mac Python3.7这时可能会出现一些错误,下面重点说一下我遇到的错误和配置:

首先是大蟒版本问题:

要用python3以上蟒蛇2好像对中文的支持不是很友好。

文件开头一定要加这个来支持中文

#-*-coding:utf-8-*-

然后用它的代码和代理地址直接到百度上找匿名代理可以用的。

当我运行时,因为我安装了python2和Python 3,我需要在vscode中将我的pythoninterpret设置为3

然后你需要在python3中运行它

python 3/Users/chong bin/Desktop/pic/Weibo _ crawler . py

跑步后,

出现错误是

URL lib . error . urlrerror : urlopenerror[SSL : CErtificate _ VERIFY _ FAILED]certificate verifyFAILED : unable getlocalissuercesertificate(_ SSL . c :1045)

寻找stackoverflow提供了以下解决方案

直接运行下面的内容,具体版本根据你的Python版本。

/应用程序/Python \ 3.7/安装\证书。命令

也就是安装一个证书,默认不安装python。

然后再运行一次。

相关阅读

  • 小贝“引爆”同济校园 警察软组织挫伤
  • 围观微博网友发起的美胸比赛学习爬取微博评论内容
  • 从爬取微博中搜索的热门事件到数据分析处理全过程(一)
  • python为什爬去微博热门事件么叫爬虫 基于python的微博数据采集
  • 热门话题榜2019年 微博热门话题排行榜
  • 爬去微博热门事件
  • 热门话题榜2019年 微博热门话题排行榜
  • python为什爬去微博热门事件么叫爬虫 基于python的微博数据采集
  • python 计算机程序设计语言
  • 版权声明:内容来源于互联网和用户投稿 如有侵权请联系删除

    本文地址:http://0561fc.cn/4221.html

    标签: #爬去微博热门事件