小编Z. *_*Lin的帖子

硒与scrapy动态页面

我正在尝试使用scrapy从网页上抓取产品信息.我的待删节网页如下所示:

  • 从包含10个产品的product_list页面开始
  • 点击"下一步"按钮加载下10个产品(两个页面之间的网址不变)
  • 我使用LinkExtractor跟踪每个产品链接到产品页面,并获得我需要的所有信息

我试图复制next-button-ajax-call但是无法正常工作,所以我试试了selenium.我可以在一个单独的脚本中运行selenium的webdriver,但我不知道如何与scrapy集成.我应该把硒部分放在我的scrapy蜘蛛里?

我的蜘蛛非常标准,如下所示:

class ProductSpider(CrawlSpider):
    name = "product_spider"
    allowed_domains = ['example.com']
    start_urls = ['http://example.com/shanghai']
    rules = [
        Rule(SgmlLinkExtractor(restrict_xpaths='//div[@id="productList"]//dl[@class="t2"]//dt'), callback='parse_product'),
        ]

    def parse_product(self, response):
        self.log("parsing product %s" %response.url, level=INFO)
        hxs = HtmlXPathSelector(response)
        # actual data follows
Run Code Online (Sandbox Code Playgroud)

任何想法都表示赞赏.谢谢!

python selenium scrapy web-scraping selenium-webdriver

74
推荐指数
1
解决办法
7万
查看次数

与django的哨兵/乌鸦:如何忽略某些例外?

我希望sentry/raven忽略来自某个函数或django模块的所有异常,但是查看文档和代码,我只看到一个选项,通过向其添加一个额外的属性来忽略自定义异常.有没有办法忽略函数名称或模块名称的异常?谢谢!

python django exception sentry

9
推荐指数
1
解决办法
2438
查看次数

Django:使用ContentType与multi_table_inheritance

我遇到了类似于 如何在Django中查询基于抽象类的对象的问题? 该线程建议使用multi_table_inheritance.我个人认为使用content_type在概念上更舒服(感觉更接近逻辑,至少对我而言)

使用上一个链接中的示例,我只需添加一个StelarType

class StellarType(models.Model):
    """
    Use ContentType so we have a single access to all types
    """
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey('content_type', 'object_id')
Run Code Online (Sandbox Code Playgroud)

然后将其添加到抽象基础模型中

class StellarObject(BaseModel):
    title = models.CharField(max_length=255)
    description = models.TextField()
    slug = models.SlugField(blank=True, null=True)
    stellartype = generic.GenericForeignKey(StellarType)
    class Meta:
        abstract = True
Run Code Online (Sandbox Code Playgroud)

要在StellarObject和StellarType之间进行同步,我们可以连接post_save信号,以便在每次创建Planet或Star时创建StellarType实例.通过这种方式,我可以通过StellarType查询StellarObjects.所以我想知道使用这种方法反对使用multi_table_inheritance的PRO和CON是什么?我认为两者都在数据库中创建了一个额外的表.但是数据库性能呢?可用性/灵活性如何?感谢您的任何投入!

django django-models

8
推荐指数
1
解决办法
931
查看次数