首页 > 解决方案 > Scrapy - href 链接中的简单文本刮刀

问题描述

我现在是 2 年的新手,所以这可能是多余的问题。我正在尝试抓取一个简单的文本,它是 href 链接中的一个故事。我尝试了 //a.text () 使用scrapy shell,但它没有刮掉任何东西。我还想输出到数据库或文本文件(scrapy 项目)。该网站是: http ://www.nderf.org/Experiences/

编辑: 现在,我可以抓取链接的标题,但不能抓取@href 标记中的文本(故事)。

import scrapy

from ..items import NdeItem


class NDESpider(scrapy.Spider):
    name='NDEstories'
    allowed_domains=["nderf.org"]
    headers=['author','desc','story']
    download_delay=1.5
    item = []

    start_urls = ["http://www.nderf.org/Archives/exceptional.html"]


    def start_requests(self):
        requests = []
        for item in self.start_urls:
            requests.append(scrapy.Request(url=item, headers={'Referer': 'https://www.google.com/'}))
            return requests


    def parse(self, response):
        #/ html / body / div[2] / section[2] / div / p / strong / span / a

         a_selectors=response.xpath('//a[@class="MiniLink"]')
         href_selector=response.xpath('@href')

         for selector in a_selectors:
            items = []
            item = NdeItem()  # type: NdeItem
            item['author'] = selector.xpath('text()').extract_first()
            item['desc'] = selector.xpath('@href').get()
            item['story'] = selector.xpath('@href/text()').get()
            items.append(item)

            # Create a new Request object
            request = response.follow(item['desc'], callback=self.parse)

            # Return it thanks to a generator

            with open ('log.txt','a') as f:
                f.write('author:{0}\n desc:{1}\n story:{2}\n'.format(item['author'],item['desc'], item['story']))
            yield request

标签: pythonpython-3.xxpathscrapy

解决方案


response.xpath('.//a/@href').get()

推荐阅读