Url在scrapy

时间:2017-04-15 16:31:11

标签: python scrapy web-crawler scrapy-spider

我是scrapy的新手,并试图废弃booking.com的酒店名称,只是玩。响应URL与请求URL不同。我也希望从所有页面获得酒店名称。

    class BookingSpider(CrawlSpider):
    name = 'booking.com_mumbai'
    allowed_domains = ['booking.com']
    start_urls = [
        'https://www.booking.com/searchresults.en-gb.html?aid=304142&label=gen173nr-1DCAEoggJCAlhYSDNiBW5vcmVmaGyIAQGYAS7CAQN4MTHIAQzYAQPoAQGSAgF5qAID&sid=73f533eb666233525bc516654c914549&checkin_month=4&checkin_monthday=26&checkin_year=2017&checkout_month=4&checkout_monthday=30&checkout_year=2017&class_interval=1&dest_id=20014181&dest_type=city&dtdisc=0&group_adults=2&group_children=0&inac=0&index_postcard=0&label_click=undef&mih=0&no_rooms=1&postcard=0&raw_dest_type=city&room1=A%2CA&sb_price_type=total&search_selected=1&src=index&src_elem=sb&ss=Los%20Angeles%2C%20California%2C%20USA%2C%20North%20America%2C%20CA&ss_all=0&ss_raw=Los%20Angeles%2C%20CA%3B&ssb=empty&sshis=0&rows=40&offset=40'
    ]

    rules = (
        Rule(LinkExtractor(allow='rows?.\w+', unique=True), follow=True, callback="parse"),
    )

    def parse_hotel_item(self):
        pass

    def parse(self, response):
        hotels = response.xpath('//*[@id="hotellist_inner"]/div')
        for hotel in hotels:
            print hotel.xpath("//h3/a/span[2]/text()").extract()
        print("Done")

日志

2017-04-15 23:40:48 [scrapy.utils.log] INFO: Scrapy 1.3.3 started (bot: scrap_booking_dot_com)
2017-04-15 23:40:48 [scrapy.utils.log] INFO: Overridden settings: {'NEWSPIDER_MODULE': 'scrap_booking_dot_com.spiders', 'SPIDER_MODULES': ['scrap_booking_dot_com.spiders'], 'ROBOTSTXT_OBEY': True, 'BOT_NAME': 'scrap_booking_dot_com'}
2017-04-15 23:40:48 [scrapy.middleware] INFO: Enabled extensions:
['scrapy.extensions.logstats.LogStats',
 'scrapy.extensions.telnet.TelnetConsole',
 'scrapy.extensions.corestats.CoreStats']
2017-04-15 23:40:48 [scrapy.middleware] INFO: Enabled downloader middlewares:
['scrapy.downloadermiddlewares.robotstxt.RobotsTxtMiddleware',
 'scrapy.downloadermiddlewares.httpauth.HttpAuthMiddleware',
 'scrapy.downloadermiddlewares.downloadtimeout.DownloadTimeoutMiddleware',
 'scrapy.downloadermiddlewares.defaultheaders.DefaultHeadersMiddleware',
 'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware',
 'scrapy.downloadermiddlewares.retry.RetryMiddleware',
 'scrapy.downloadermiddlewares.redirect.MetaRefreshMiddleware',
 'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware',
 'scrapy.downloadermiddlewares.redirect.RedirectMiddleware',
 'scrapy.downloadermiddlewares.cookies.CookiesMiddleware',
 'scrapy.downloadermiddlewares.stats.DownloaderStats']
2017-04-15 23:40:48 [scrapy.middleware] INFO: Enabled spider middlewares:
['scrapy.spidermiddlewares.httperror.HttpErrorMiddleware',
 'scrapy.spidermiddlewares.offsite.OffsiteMiddleware',
 'scrapy.spidermiddlewares.referer.RefererMiddleware',
 'scrapy.spidermiddlewares.urllength.UrlLengthMiddleware',
 'scrapy.spidermiddlewares.depth.DepthMiddleware']
2017-04-15 23:40:48 [scrapy.middleware] INFO: Enabled item pipelines:
[]
2017-04-15 23:40:48 [scrapy.core.engine] INFO: Spider opened
2017-04-15 23:40:48 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2017-04-15 23:40:48 [scrapy.extensions.telnet] DEBUG: Telnet console listening on 127.0.0.1:6023
2017-04-15 23:40:49 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.booking.com/robots.txt> (referer: None)
2017-04-15 23:40:49 [scrapy.downloadermiddlewares.redirect] DEBUG: Redirecting (302) to <GET https://www.booking.com/searchresults.en-gb.html?aid=304142&label=gen173nr-1DCAEoggJCAlhYSDNiBW5vcmVmaGyIAQGYAS7CAQN4MTHIAQzYAQPoAQGSAgF5qAID&sid=73f533eb666233525bc516654c914549&checkin_month=4&checkin_monthday=26&checkin_year=2017&checkout_month=4&checkout_monthday=30&checkout_year=2017&class_interval=1&dest_id=20014181&dest_type=city&dtdisc=0&group_adults=2&group_children=0&inac=0&index_postcard=0&label_click=undef&mih=0&no_rooms=1&postcard=0&raw_dest_type=city&room1=A%2CA&sb_price_type=total&search_selected=1&src=index&src_elem=sb&ss=Los%20Angeles%2C%20California%2C%20USA%2C%20North%20America%2C%20CA&ss_all=0&ss_raw=Los%20Angeles%2C%20CA;&ssb=empty&sshis=0&rows=40&offset=40> from <GET https://www.booking.com/searchresults.en-gb.html?aid=304142&label=gen173nr-1DCAEoggJCAlhYSDNiBW5vcmVmaGyIAQGYAS7CAQN4MTHIAQzYAQPoAQGSAgF5qAID&sid=73f533eb666233525bc516654c914549&checkin_month=4&checkin_monthday=26&checkin_year=2017&checkout_month=4&checkout_monthday=30&checkout_year=2017&class_interval=1&dest_id=20014181&dest_type=city&dtdisc=0&group_adults=2&group_children=0&inac=0&index_postcard=0&label_click=undef&mih=0&no_rooms=1&postcard=0&raw_dest_type=city&room1=A%2CA&sb_price_type=total&search_selected=1&src=index&src_elem=sb&ss=Los%20Angeles%2C%20California%2C%20USA%2C%20North%20America%2C%20CA&ss_all=0&ss_raw=Los%20Angeles%2C%20CA%3B&ssb=empty&sshis=0&rows=40&offset=40>
2017-04-15 23:40:49 [scrapy.downloadermiddlewares.redirect] DEBUG: Redirecting (301) to <GET https://www.booking.com/searchresults.en-gb.html?dest_id=20014181;dest_type=city;offset=30;ss=Los%2520Angeles%252C%2520California%252C%2520USA%252C%2520North%2520America%252C%2520CA> from <GET https://www.booking.com/searchresults.en-gb.html?aid=304142&label=gen173nr-1DCAEoggJCAlhYSDNiBW5vcmVmaGyIAQGYAS7CAQN4MTHIAQzYAQPoAQGSAgF5qAID&sid=73f533eb666233525bc516654c914549&checkin_month=4&checkin_monthday=26&checkin_year=2017&checkout_month=4&checkout_monthday=30&checkout_year=2017&class_interval=1&dest_id=20014181&dest_type=city&dtdisc=0&group_adults=2&group_children=0&inac=0&index_postcard=0&label_click=undef&mih=0&no_rooms=1&postcard=0&raw_dest_type=city&room1=A%2CA&sb_price_type=total&search_selected=1&src=index&src_elem=sb&ss=Los%20Angeles%2C%20California%2C%20USA%2C%20North%20America%2C%20CA&ss_all=0&ss_raw=Los%20Angeles%2C%20CA;&ssb=empty&sshis=0&rows=40&offset=40>
2017-04-15 23:40:50 [scrapy.downloadermiddlewares.redirect] DEBUG: Redirecting (301) to <GET https://www.booking.com/searchresults.en-gb.html?dest_id=20014181;dest_type=city;offset=30;ss=Los%20Angeles%2C%20California%2C%20USA%2C%20North%20America%2C%20CA> from <GET https://www.booking.com/searchresults.en-gb.html?dest_id=20014181;dest_type=city;offset=30;ss=Los%2520Angeles%252C%2520California%252C%2520USA%252C%2520North%2520America%252C%2520CA>
2017-04-15 23:40:51 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.booking.com/searchresults.en-gb.html?dest_id=20014181;dest_type=city;offset=30;ss=Los%20Angeles%2C%20California%2C%20USA%2C%20North%20America%2C%20CA> (referer: None)

我得到了不同的结果。

1 个答案:

答案 0 :(得分:0)

根据您的日志和源代码,您的刮刀正在

开始

https://www.booking.com/searchresults.en-gb.html?dest_id=20014181;dest_type=city;offset=30;ss=Los%20Angeles%2C%20California%2C%20USA%2C%20North%20America%2C%20CA

然后重定向多次,以便最终抓取

https://www.booking.com/searchresults.en-gb.html?aid=304142&label=gen173nr-1DCAEoggJCAlhYSDNiBW5vcmVmaGyIAQGYAS7CAQN4MTHIAQzYAQPoAQGSAgF5qAID&sid=73f533eb666233525bc516654c914549&checkin_month=4&checkin_monthday=26&checkin_year=2017&checkout_month=4&checkout_monthday=30&checkout_year=2017&class_interval=1&dest_id=20014181&dest_type=city&dtdisc=0&group_adults=2&group_children=0&inac=0&index_postcard=0&label_click=undef&mih=0&no_rooms=1&postcard=0&raw_dest_type=city&room1=A%2CA&sb_price_type=total&search_selected=1&src=index&src_elem=sb&ss=Los%20Angeles%2C%20California%2C%20USA%2C%20North%20America%2C%20CA&ss_all=0&ss_raw=Los%20Angeles%2C%20CA%3B&ssb=empty&sshis=0&rows=40&offset=40

这并没有错。

为什么?

这就是booking.com处理你的起始网址的方式。刮刀没什么特别的,尝试在浏览器中访问启动网址时也是如此。

只需将您的初始网址粘贴到浏览器中,您就会看到浏览器的网址行更改为几乎相同的长网址。

所以回复网址很好。

真正的问题来自for循环中的xpath "//h3/a/span[2]/text()"

它有两个问题:

  1. 因为xpath以&#39; /&#39;开头它将在根对象上工作,而你最希望它可以处理你正在迭代的每个酒店元素。
  2. 修复方法是添加&#39;。&#39;到xpath的前面(类似于在文件系统上导航文件夹时使用&#39;。&#39;。)

    示例:".//h3/a/span[2]/text()"

    1. 尽管如此,我还是无法使你的xpath工作。精心设计了以下xpath,它适用于我:".//span[@class='sr-hotel__name']/text()"
    2. (一个好的xpath应该更好地依赖于id和类名,而不是特定的元素嵌套,因为前者的更改风险较小)