美文网首页
08 如何用爬虫下载所有微博收藏帖

08 如何用爬虫下载所有微博收藏帖

作者: 夏威夷的芒果 | 来源:发表于2018-08-15 18:22 被阅读5次
#下载地址:https://video.mugglecode.com/xcookie.py
c = '''fcc0; TC002'''  # 这里请换成你的cookie
cookie_list = []
for one in c.split('; '):
    k,v = one.split('=',1)
    cookie_list.append({'name':k, 'value':v})
print(cookie_list)

正经的代码

这里用字典形式添加cookie,然后调用add_cookie()添加cookie就可以了。

# 代码中用到的xcookie.py模板请在这里下载: https://video.mugglecode.com/xcookie.py

from selenium.webdriver import Chrome
from bs4 import BeautifulSoup
import time
import sys

class Spider:   #爬虫对象

    def __init__(self, index_url):
        self.index_url = index_url
        self.raw_htmls = []
        self.boot()

    def boot(self):
        self.chrome = Chrome(executable_path='./chromedriver')
        self.chrome.start_client()
        self.check_cookie()

    def check_cookie(self):
        from xcookie import cookie_list   #从文件里导入cookie
        if cookie_list:
            self.chrome.get(self.index_url)        #打开url
            time.sleep(5)
            self.chrome.delete_all_cookies()     #删除一切的cookies
            print('Clear!')
            for c in cookie_list:     
                self.chrome.add_cookie(c)         #逐条添加cookies
            print('Done')
        else:
            print('pls add cookie first')
            sys.exit()


    def crawl(self, target_url):
        self.chrome.get(target_url)
        print('Wait for web page loading')
        time.sleep(2)
        self.raw_htmls.append(self.chrome.page_source)    #那么多网页先都拿下来



class Parser:

    def __init__(self, html_list):
        self.html_list = html_list
        self.raw_posts = []
        self.parse()


    def parse(self):
        for html in self.html_list:
            soup = BeautifulSoup(html,'html.parser')
            detail_sel = '.WB_detail'    #拿下来weibo
            detail_els = soup.select(detail_sel)
            for detail in detail_els:
                content = detail.get_text()   #可以拿下多级的文字
                clean_text = content.replace(' ','').replace('\n','')
                self.raw_posts.append(clean_text)
        print(self.raw_posts)


    def save_into_text(self):
        with open('./fav.txt','a+') as f:
            for i in self.raw_posts:
                f.write(i)
                f.write('\n')
                f.write('---'*10)
                f.write('\n')


s = Spider(index_url='https://www.weibo.com')
s.crawl(target_url='https://www.weibo.com/fav')
p = Parser(s.raw_htmls)
p.save_into_text()


time.sleep(9999)

注意,如果是手机的页面,必须用触摸

from selenium.webdriver.common.touch_actions import TouchActions # 用来模拟手机端操作

相关文章

  • 08 如何用爬虫下载所有微博收藏帖

    正经的代码 这里用字典形式添加cookie,然后调用add_cookie()添加cookie就可以了。 注意,如果...

  • 【工具】echarts+kuno+分词

    数据: python爬虫:微博爬虫、借助'出书啦'爬微信知乎Java爬虫:Java微博爬虫 时间轴: JAVA时间...

  • 微博爬虫

    WeiBoSpider 简介一个基于 python3.6.8 和 splinter 的微博爬虫,可以爬取指定日期...

  • 新浪微博爬虫

    layout: posttitle: 新浪微博爬虫categories: Spiderdescription: 微...

  • 2018-08《如何用数据解决实际问题》[www.rejoice

    下载地址:2018-08《如何用数据解决实际问题》[www.rejoiceblog.com].pdf

  • 如何采集新浪微博数据?

    本文主要介绍神箭手“新浪微博采集爬虫”(以下简称“微博爬虫”)的使用教程以及注意事项。 新浪微博中有大量高价值的软...

  • Python实践与学习索引

    爬虫小专栏—爬取广州二手房信息小专栏—爬虫模块化小专栏—广度优先爬虫小专栏—爬取某个用户的所有微博包简书—pandas

  • 微博收藏

    [cp]#研途有悟[超话]# 倒计时一下子跳到了50天,已经记不清去年这个时候自己的进度。今年最大的感触就是千万别...

  • 微博收藏

    我发现自己越来越喜欢收藏,而不喜欢看那些收藏的东西了。收藏夹里已经堆了足足2000多条内容,我几乎就没有想起来过。...

  • 下载微博

    微博还是下载了,搜就搜吧,不能因为一个无关紧要的碧池放弃整个微博海洋

网友评论

      本文标题:08 如何用爬虫下载所有微博收藏帖

      本文链接:https://www.haomeiwen.com/subject/xktwbftx.html