加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 我怎么能告诉Scrapy只抓取Xpath中的链接?

发布时间:2020-12-16 22:53:30 所属栏目:Python 来源:网络整理
导读:我是Scrapy的新手,我想做的是创建一个只跟踪给定start_urls上 HTML元素内部链接的爬虫 就像一个例子,我只想让一个爬行器通过将start_urls设置为https://www.airbnb.com/s?location=New+York%2C+NYcheckin=checkout=guests=1的AirBnB列表 而不是抓取URL中的所
我是Scrapy的新手,我想做的是创建一个只跟踪给定start_urls上 HTML元素内部链接的爬虫

就像一个例子,我只想让一个爬行器通过将start_urls设置为https://www.airbnb.com/s?location=New+York%2C+NY&checkin=&checkout=&guests=1的AirBnB列表

而不是抓取URL中的所有链接,我只想抓取xpath内的链接// * [@ id =“results”]

目前我正在使用以下代码来抓取所有链接,我如何才能使其仅适用于抓取// * [@ id =“results”]

from scrapy.selector import HtmlXPathSelector
    from tutorial.items import DmozItem
    from scrapy.contrib.spiders import CrawlSpider,Rule
    from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
    from scrapy.selector import HtmlXPathSelector


    class BSpider(CrawlSpider):
            name = "bt"
            #follow = True
            allowed_domains = ["mydomain.com"]
            start_urls = ["http://myurl.com/path"]
            rules =(Rule(SgmlLinkExtractor(allow = ()),callback = 'parse_item',follow=True),)


        def parse_item(self,response):
        {parse code}

正确方向的任何提示将非常感激,
谢谢!

解决方法

您可以将restrict_xpaths关键字参数传递给SgmlLinkExtractor.从 the docs开始:

> restrict_xpaths(str或list) – 是一个XPath(或XPath列表),用于定义响应中应从中提取链接的区域.如果给定,则仅扫描由这些XPath选择的文本以获取链接.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读