为什么用scarpy爬大众点评的城市主页有内容而按区爬的时候就无法获取内容?

发布于 2022-09-07 08:50:20 字数 6408 浏览 20 评论 0

如下图以所示,当页面是整个城市的美食板块的时候,例如西安美食的网址是"http://www.dianping.com/xian/ch10",可以正常爬取到数据(如图一)。
图片描述
但是由于页面限制50页,随就尝试根据各区和各美食种类爬取,当将页面改为所选区及美食种类后,例如下列地址“http://www.dianping.com/xian/...“,便无法获取数据了。但是网站是能正常打开的。
图片描述
拜托大神看一下究竟是哪里出错了!万分感激。

我尝试了用 scrapy shell跑这个网站,但是用view(response)打开的是一个空的txt文档,不是打开一个网页。

以下是我的代码
dianping.py

import scrapy
import requests
from scrapy.selector import Selector
from ..items import DianpingItem


class DianpingSpider(scrapy.Spider):
    name = "dianping"
    start_urls = ['http://www.dianping.com/xian/ch10/r8915/']

    def parse(self, response):
        Dianping = DianpingItem()
        title_list = response.xpath("//div[@class='tit']/a[1]/h4/text()").extract()
        level_list = response.xpath("//div[@class='comment']/span[1]/@title").extract()
        comment_list = response.xpath("//div[@class='comment']/a[1]/b/text()").extract()
        price_list = response.xpath("//div[@class='comment']/a[2]/b/text()").extract()
        kouwei_list = response.xpath("//span[@class='comment-list']/span[1]/b/text()").extract()
        huanjing_list = response.xpath("//span[@class='comment-list']/span[2]/b/text()").extract()
        fuwu_list = response.xpath("//span[@class='comment-list']/span[2]/b/text()").extract()
        caixi_list = response.xpath("//div[@class='tag-addr']/a[1]/span/text()").extract()
        area_list = response.xpath("//div[@class='tag-addr']/a[2]/span/text()").extract()
        address_list = response.xpath("//div[@class='tag-addr']//span[@class='addr']/text()").extract()
        recommend_list = response.xpath("//div[@class='tit']/a[1]/h4/text()").extract()
        # recommend_list2 = recommend_list1[0].xpath('string(.)').extract()
        # recommend_list = [item.replace(' ', '').replace('\n', '|') for item in recommend_list2]
        for i1, i2, i3, i4, i5,i6,i7,i8,i9,i10,i11 in zip(title_list,level_list,comment_list,price_list,kouwei_list,huanjing_list,fuwu_list,caixi_list,area_list,address_list,recommend_list):
            Dianping['title'] = i1
            Dianping['level'] = i2
            Dianping['comment'] = i3
            Dianping['price'] = i4
            Dianping['kouwei'] = i5
            Dianping['huanjing'] = i6
            Dianping['fuwu'] = i7
            Dianping['caixi'] = i8
            Dianping['area'] = i9
            Dianping['address'] = i10
            Dianping['recommend'] = i11
            yield Dianping
        next_pages = response.xpath("//div[@class='page']/a[@class='next']/@href").extract()
        if next_pages:
            yield scrapy.Request(next_pages[0], callback=self.parse)

setting.py

# -*- coding: utf-8 -*-

# Scrapy settings for dianping project
#
# For simplicity, this file contains only settings considered important or
# commonly used. You can find more settings consulting the documentation:
#
#     https://doc.scrapy.org/en/latest/topics/settings.html
#     https://doc.scrapy.org/en/latest/topics/downloader-middleware.html
#     https://doc.scrapy.org/en/latest/topics/spider-middleware.html

BOT_NAME = 'dianping'

SPIDER_MODULES = ['dianping.spiders']
NEWSPIDER_MODULE = 'dianping.spiders'
USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36'
# Crawl responsibly by identifying yourself (and your website) on the user-agent
#USER_AGENT = 'dianping (+http://www.yourdomain.com)'

# Obey robots.txt rules
ROBOTSTXT_OBEY = True

# Configure maximum concurrent requests performed by Scrapy (default: 16)
# CONCURRENT_REQUESTS = 1

# Configure a delay for requests for the same website (default: 0)
# See https://doc.scrapy.org/en/latest/topics/settings.html#download-delay
# See also autothrottle settings and docs
DOWNLOAD_DELAY = 5
# The download delay setting will honor only one of:
#CONCURRENT_REQUESTS_PER_DOMAIN = 16
#CONCURRENT_REQUESTS_PER_IP = 16

# Disable cookies (enabled by default)
COOKIES_ENABLED = True

# Disable Telnet Console (enabled by default)
#TELNETCONSOLE_ENABLED = False

# Override the default request headers:
DEFAULT_REQUEST_HEADERS = {
      'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
      'Accept-Language': 'en-US,en;q=0.9',
      'Accept-Encoding': 'gzip, deflate'
}

# Enable or disable spider middlewares
# See https://doc.scrapy.org/en/latest/topics/spider-middleware.html
#SPIDER_MIDDLEWARES = {
#    'dianping.middlewares.DianpingSpiderMiddleware': 543,
#}

# Enable or disable downloader middlewares
# See https://doc.scrapy.org/en/latest/topics/downloader-middleware.html
# DOWNLOADER_MIDDLEWARES = {
#     'dianping.middlewares.DianpingDownloaderMiddleware': 543,
# }
# DOWNLOADER_MIDDLEWARES = {
#      'scrapy.contrib.downloadermiddleware.httpproxy.HttpProxyMiddleware': 543,
#      'dianping.middlewares.ProxyMiddleWare': 125,
#      'dianping.middlewares.DianpingDownloaderMiddleware': 543,
# }
# Enable or disable extensions
# See https://doc.scrapy.org/en/latest/topics/extensions.html
#EXTENSIONS = {
#    'scrapy.extensions.telnet.TelnetConsole': None,
#}

# Configure item pipelines
# See https://doc.scrapy.org/en/latest/topics/item-pipeline.html
ITEM_PIPELINES = {
    'dianping.pipelines.DianpingPipeline': 300,
}

# Enable and configure the AutoThrottle extension (disabled by default)
# See https://doc.scrapy.org/en/latest/topics/autothrottle.html
#AUTOTHROTTLE_ENABLED = True
# The initial download delay
#AUTOTHROTTLE_START_DELAY = 5
# The maximum download delay to be set in case of high latencies
#AUTOTHROTTLE_MAX_DELAY = 60
# The average number of requests Scrapy should be sending in parallel to
# each remote server
#AUTOTHROTTLE_TARGET_CONCURRENCY = 1.0
# Enable showing throttling stats for every response received:
#AUTOTHROTTLE_DEBUG = False

# Enable and configure HTTP caching (disabled by default)
# See https://doc.scrapy.org/en/latest/topics/downloader-middleware.html#httpcache-middleware-settings
#HTTPCACHE_ENABLED = True
#HTTPCACHE_EXPIRATION_SECS = 0
#HTTPCACHE_DIR = 'httpcache'
#HTTPCACHE_IGNORE_HTTP_CODES = []
#HTTPCACHE_STORAGE = 'scrapy.extensions.httpcache.FilesystemCacheStorage'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

音栖息无 2022-09-14 08:50:20

ROBOTSTXT_OBEY = False 再试试

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文