【问题标题】:Trying to make a recursive crawl spider with python. SyntaxError: non-keyword arg after keyword arg试图用python制作一个递归爬虫。语法错误:关键字 arg 之后的非关键字 arg
【发布时间】:2015-04-13 17:35:27
【问题描述】:

我试图在 scrappy 中抓取一个以上的页面,我的函数确实返回了第一个起始 url,但我无法使蜘蛛的规则工作。

这是我目前所拥有的:

import scrapy

from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from craigslist_sample.items import CraigslistSampleItem



class MySpider(CrawlSpider):
    name = "craigs"
    allowed_domains = ["craigslist.org"]
    start_urls = ["http://sfbay.craigslist.org/npo/"]



    rules = (
        Rule(SgmlLinkExtractor(allow=('.*?s=.*',), restrict_xpaths('a[@class="button next"]',)), callback='parse', follow=True),)

    def parse(self, response):
        for sel in response.xpath('//span[@class="pl"]'):
            item = CraigslistSampleItem()
            item['title'] = sel.xpath('a/text()').extract()
            item['link'] = sel.xpath('a/@href').extract()
            yield item`

我收到这个错误

SyntaxError: 关键字 arg 后的非关键字 arg

更新:

感谢下面的回答。没有语法错误,但是我的爬虫只是停留在同一个页面,不爬。

更新代码

import scrapy

from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from craigslist_sample.items import CraigslistSampleItem
from scrapy.contrib.linkextractors import LinkExtractor


class MySpider(CrawlSpider):
    name = "craigs"
    allowed_domains = ["craigslist.org"]
    start_urls = ["http://sfbay.craigslist.org/npo/"]

    rules = (Rule(SgmlLinkExtractor(allow=['.*?s=.*'], restrict_xpaths=('a[@class="button next"]')), 
        callback='parse', follow=True, ),
)


    def parse(self, response):
        for sel in response.xpath('//span[@class="pl"]'):
            item = CraigslistSampleItem()
            item['title'] = sel.xpath('a/text()').extract()
            item['link'] = sel.xpath('a/@href').extract()
            yield item

【问题讨论】:

  • 应该是restrict_xpaths=('a[@class="button next"]',))?看起来你可能忘记了=
  • 是的,我检查并修复了语法错误,但仍然没有抓取其他页面。

标签: python syntax-error scrapy


【解决方案1】:

您的问题与此类似(Python 3)

>>> print("hello")
hello
>>> print("hello", end=",,")
hello,,
>>> print(end=",,", "hello")
SyntaxError: non-keyword arg after keyword arg

行:

Rule(SgmlLinkExtractor(allow=('.*?s=.*',), restrict_xpaths('a[@class="button next"]',)), callback='parse', follow=True),)

必须被称为:

Rule(SgmlLinkExtractor(restrict_xpaths('a[@class="button next"]'),allow=('.*?s=.*',)), callback='parse', follow=True),)

【讨论】:

  • 它给了我这个输出 NameError: name 'restrict_xpaths' is not defined
  • @2one2 这个答案可能是相关的stackoverflow.com/questions/14394432/…
  • 好吧,我没有语法错误,但我的代码没有抓取其他页面,只是开始 url
  • @2one2 试着考虑一下,如果你不能让它工作,那么再问一个问题。
【解决方案2】:

好的,所以我发现我使用 parse 方法的问题是什么:

def parse(self, response):
    for sel in response.xpath('//span[@class="pl"]'):
        item = CraigslistSampleItem()
        item['title'] = sel.xpath('a/text()').extract()
        item['link'] = sel.xpath('a/@href').extract()
        yield item 

读完后我发现了我的问题。 http://doc.scrapy.org/en/latest/topics/spiders.html#scrapy.contrib.spiders.CrawlSpider

CrawlSpider 使用 parse 作为方法,所以我不得不将我的函数重命名为:

def parse_item(self, response):
    for sel in response.xpath('//span[@class="pl"]'):
        item = CraigslistSampleItem()
        item['title'] = sel.xpath('a/text()').extract()
        item['link'] = sel.xpath('a/@href').extract()
        yield item 

【讨论】:

    猜你喜欢
    • 2017-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多