关于Scrapy与callback

这几天为了面试的事情,看个很多关于Scrapy以及周边的相关技术的文章和代码,相关的整理如下:

  • Scrapy爬取很多网站的方法:

编程方式下运行 Scrapy spider
使用Scrapy定制可动态配置的爬虫
使用Redis和SQLAlchemy对Scrapy Item去重并存储

这系列的文章对之后的爬虫工作极为有用,值得很大程度的借鉴

  • Scrapy的改造

使用SQLAlchemy
scrapy-redis
scrapinghub

  • 回调与异步

维基百科-callback
Python: yield 的黑魔法

ps:  这里面这段代码极好!!

from collections import defaultdict

def find_anagram(input_data):
    """ find all the anagram from a dictionary
    """

    # initize a cotoutine
    def start(func):
        def _(*args, **kwargs):
            g = func(*args, **kwargs)
            g.send(None)
            return g
        return _

    @start
    def sign(target):
        while True:
            words = yield
            for w in words:
                target.send([''.join(sorted(w)), w])
            target.send(None)  # flag incicates that all data have been seen

    @start
    def sort(target):
        sign_words = []
        while True:
            word = yield
            if word:
                sign_words.append(word)
            else:  # all word have sort
                target.send(sorted(sign_words))

    @start
    def squash():
        nonlocal dictionary  # python3 only: use the variable define in outside
        while True:
            word_list = yield
            for x in word_list:
                dictionary[x[0]].add(x[1])

    dictionary = defaultdict(set)

    sign(sort(squash())).send(input_data)

    # filter the word has no anagram
    return filter(lambda x: len(x[1]) > 1, dictionary.items())


if __name__ == "__main__":

    test_data = ['abc', 'acb', 'bca', 'iterl', 'liter', 'hello',
                 'subessential', 'suitableness', 'hello']
    result = find_anagram(test_data)
    for each in result:
        print(each[0], ':', each[1])
  • ajax爬取的问题

使用PhantomJS 和Selenium 模拟浏览器行为爬取网页
ScrapyJS

  • 大神的笔记

使用python,scrapy写(定制)爬虫的经验,资料,杂。

以上是我这两天所看到的精华,还需要我消化两天。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • scrapy学习笔记(有示例版) 我的博客 scrapy学习笔记1.使用scrapy1.1创建工程1.2创建爬虫模...
    陈思煜阅读 12,798评论 4 46
  • 爬虫文章 in 简书程序员专题: like:128-Python 爬取落网音乐 like:127-【图文详解】py...
    喜欢吃栗子阅读 22,068评论 4 411
  • Selenium的Webdriver爬取动态网页效果虽然不错,但效率方面并不如人意。最近一直研究如何提高动态页面爬...
    Rabin_xie阅读 8,689评论 10 43
  • 清晨,冷雨敲窗惊渺残梦。听耳边乒乓做响……下冰雹了?!哦,又是楼下那家阳台罩子上的塑料雨搭做怪。明明是牛毛细雨,积...
    舞动禅心阅读 442评论 0 1
  • 那年小弟才4岁多,天真烂漫的年纪,肥嘟嘟的很是讨人喜欢。开春时节,家中大人忙,无暇看护!带他的任务就交给了正在...
    花束DD阅读 218评论 0 0