龙空技术网

微博爬虫,python微博用户主页小姐姐图片内容采集爬虫

流光易趣图 72

前言:

现在看官们对“新浪微博爬虫”都比较珍视,看官们都需要学习一些“新浪微博爬虫”的相关资讯。那么小编也在网摘上搜集了一些关于“新浪微博爬虫””的相关文章,希望姐妹们能喜欢,姐妹们快快来学习一下吧!

python爬虫,微博爬虫,需要知晓微博用户id号,能够通过抓取微博用户主页内容来获取用户发表的内容,时间,点赞数,转发数等数据,当然以上都是本渣渣结合网上代码抄抄改改获取的!

要抓取的微博地址:

BUT,我们实际应用的抓取地址:(移动端的微博地址)

LSP的最爱,各种小姐姐,随你任意爬取,快收藏起来啊!

通过浏览器抓包,我们可以获悉几个比较重要的参数:

type: uidvalue: 5118612601containerid: 1005055118612601

其实还有一个比较重要的参数,那就是翻页:'page':page!

还有一个SSL错误问题,大家可以自行处理!

import logginglogging.captureWarnings(True)

# 屏蔽warning信息requests.packages.urllib3.disable_warnings()

html=requests.get(self.url,headers=self.headers,params=params,timeout=5,verify=False).content.decode('utf-8')

几个关键点获取 containerid 参数

def get_containerid(self): url = f'{self.uid}' data = requests.get(url,headers=self.headers,timeout=5,verify=False).content.decode('utf-8') content = json.loads(data).get('data') for data in content.get('tabsInfo').get('tabs'): if (data.get('tab_type') == 'weibo'): containerid = data.get('containerid')

self.containerid=containerid获取 微博用户发表 数据

def get_content(self,i): params={ 'type': 'uid', 'value': self.uid, 'containerid': self.containerid, 'page':i, } html=requests.get(self.url,headers=self.headers,params=params,timeout=5,verify=False).content.decode('utf-8') data=json.loads(html)['data'] cards=data['cards'] #print(cards) j = 1 for card in cards: if "mblog" in str(card): mblog = card['mblog'] raw_text = mblog['raw_text'] # 文本内容 print(raw_text) scheme=card['scheme'] #微博链接 attitudes_count = mblog.get('attitudes_count') #点赞数 comments_count = mblog.get('comments_count') #评论数 created_at = mblog.get('created_at') #发布时间 reposts_count = mblog.get('reposts_count') #转发数 print(scheme) img_path=f'{self.path}{i}/{j}' os.makedirs(f'{img_path}/',exist_ok=True) with open(f'{img_path}/{j}.txt', 'a', encoding='utf-8') as f: f.write(f'{raw_text}')

img_urls=[] if mblog.get('pics') != None: img_datas=mblog['pics'] for img_data in img_datas: img_url=img_data['large']['url'] img_urls.append(img_url) print(img_urls)

#多线程下载图片 self.get_imgs(img_urls,img_path)

#多进程下载图片 #self.get_pimgs(img_urls)

with open(f'{self.uid}/{self.uid}.txt', 'a', encoding='utf-8') as fh: fh.write("----第" + str(i) + "页,第" + str(j) + "条微博----" + "\n") fh.write(f"微博地址: {str(scheme)}\n微博内容:{raw_text}\n" f"发布时间:{str(created_at)}\n转发数:{str(reposts_count)}\n" f"点赞数:{str(attitudes_count)}\n评论数:{str(comments_count)}\n\n") j=j+1

time.sleep(2)

多线程下载图片

#多线程下载图片 def get_imgs(self,img_urls,img_path): threadings = [] for img_url in img_urls: t = threading.Thread(target=self.get_img, args=(img_url,img_path)) threadings.append(t) t.start()

for x in threadings: x.join()

print("多线程下载图片完成")

def get_img(self, img_url,img_path): img_name = img_url.split('/')[-1] print(f'>> 正在下载图片:{img_name} ..') r = requests.get(img_url, timeout=8, headers=self.headers,verify=False) with open(f'{img_path}/{img_name}', 'wb') as f: f.write(r.content) print(f'>> 图片:{img_name} 下载完成!')

本来还想搞个多进程,结果翻车了,报错各种头秃,那就不搞了!!

手里头有二份微博爬虫的源码,不同的爬取地址和思路,一起分享给大家,仅供参考学习!

一份还包含GUI界面,当然这是本渣渣参考的主要来源代码!

​本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理

私信小编01即可获取大量Python学习资料

标签: #新浪微博爬虫