我已经实现了如下爬虫程序。
它正在工作,并将通过link extractor规定的站点。
基本上我要做的是从页面的不同位置提取信息:
-“新闻”(如果存在)下的HREF和THECT()
-“思考块”类下的图像URL(如果存在)
我有三个问题要解决:
1)复制LinkExtractor
似乎它将复制处理过的页。(我查看了导出文件,发现同一个~.img出现了很多次,但几乎不可能)
事实上,对于网站中的每一个页面,底部都有超链接,方便用户直接指向他们感兴趣的主题,而我的目标是从主题页面(此处列出了同一主题下的几个段落标题)和段落页面中的图像(您可以到达点击主题页上的文章标题。
我怀疑在这种情况下,链接提取器会再次循环同一页。
(可能用深度极限来求解?)
2)改进解析项
我认为这对parse_item是不太有效的。我该怎么改进呢?我需要从Web中的不同地方提取信息(当然,它只提取它是否存在)。此外,看起来PARSEBY项只能处理HKJIMAGE,而不是HkejItem(同样我用输出文件检查)。我该怎么处理?
3)我需要蜘蛛能读中文。
我在香港的一个网站上爬行,能够阅读中文是很重要的。
网站:
http://www1.hkej.com/dailynews/headline/article/1105148/IMF%E5%82%B3%E4%BF%83%E4%B8%AD%E5%9C%8B%E9%80%80%E5%87%BA%E6%95%91%E5%B8%82
只要它属于《每日新闻》,那就是我想要的。

from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.selector import Selector
from scrapy.http import Request, FormRequest
from scrapy.contrib.linkextractors import LinkExtractor
import items


class EconjournalSpider(CrawlSpider):
    name = "econJournal"
    allowed_domains = ["hkej.com"]
    login_page = 'http://www.hkej.com/template/registration/jsp/login.jsp'
    start_urls =  'http://www.hkej.com/dailynews'

    rules=(Rule(LinkExtractor(allow=('dailynews', ),unique=True), callback='parse_item', follow =True),
           )


    def start_requests(self):
         yield Request(
         url=self.login_page,
         callback=self.login,
         dont_filter=True
         )
# name column
    def login(self, response):
        return FormRequest.from_response(response,
                    formdata={'name': 'users', 'password': 'my password'},
                    callback=self.check_login_response)

    def check_login_response(self, response):
        """Check the response returned by a login request to see if we are
        successfully logged in.
        """
        if "username" in response.body:
            self.log("\n\n\nSuccessfully logged in. Let's start crawling!\n\n\n")
            return Request(url=self.start_urls)
        else:
            self.log("\n\n\nYou are not logged in.\n\n\n")
            # Something went wrong, we couldn't log in, so nothing happens

    def parse_item(self, response):
        hxs = Selector(response)
        news=hxs.xpath("//div[@class='news']")
        images=hxs.xpath('//p')

        for image in images:
            allimages=items.HKejImage()
            allimages['image'] = image.xpath('a/img[not(@data-original)]/@src').extract()
            yield allimages

        for new in news:
            allnews = items.HKejItem()
            allnews['news_title']=new.xpath('h2/@text()').extract()
            allnews['news_url'] = new.xpath('h2/@href').extract()
            yield allnews

非常感谢您的帮助!

最佳答案

首先,要设置,可以在settings.py文件中设置,或者可以在spider上指定custom_settings参数,例如:

custom_settings = {
    'DEPTH_LIMIT': 3,
}

然后,你必须确保蜘蛛达到了parse_item方法(我认为它没有达到,还没有测试)。而且您也不能在规则上指定callbackfollow参数,因为它们不能一起工作。
首先删除规则上的follow,或添加另一个规则,以检查要遵循的链接以及要作为项返回的链接。
其次,在parse_item方法中,您得到的xpath不正确,要获得所有图像,您可能需要使用如下方法:
images=hxs.xpath('//img')

然后要获取图像url:
allimages['image'] = image.xpath('./@src').extract()

就新闻而言,这似乎是可行的:
allnews['news_title']=new.xpath('.//a/text()').extract()
allnews['news_url'] = new.xpath('.//a/@href').extract()

现在,正如理解您的问题一样,这不是一个Linkextractor duplicating错误,而是一个糟糕的规则规范,也要确保您有有效的xpath,因为您的问题并不表示您需要xpath更正。

09-10 16:53