【问题标题】:Unable to scrape snapdeal data using scrapy无法使用 scrapy 抓取 snapdeal 数据
【发布时间】:2018-06-10 12:45:38
【问题描述】:

尝试抓取 snapdeal 数据时的输出如下:

scrapy shell "https://www.snapdeal.com"

response.text

u'<HTML><HEAD>\n<TITLE>Access Denied</TITLE>\n</HEAD><BODY>\n<H1>Access Denied</H1>\n \nYou don\'t have permission to access "http&#58;&#47;&#47;www&#46;snapdeal&#46;com&#47;" on this server.<P>\nReference&#32;&#35;18&#46;1dd70b17&#46;1514632273&#46;17456300\n</BODY>\n</HTML>\n'

有什么帮助吗?

【问题讨论】:

  • 这是刮保护,他们不想让你刮。您需要使用代理并使用其他一些用户代理,scrapy shell 将使用默认的scrapy用户代理
  • 你必须复制整个请求并在scrapy中模仿,

标签: python html web-scraping scrapy


【解决方案1】:

如果我使用User-Agent,那么我会得到正确的页面

scrapy shell

fetch("https://www.snapdeal.com", headers={'User-Agent': "Mozilla/5.0"})

response.text

或者使用脚本

import scrapy
#from scrapy.commands.view import open_in_browser

class MySpider(scrapy.Spider):

    name = 'myspider'

    start_urls = ['https://www.snapdeal.com/']

    def parse(self, response):
        print('url:', response.url)

        #open_in_browser(response)

        for item in response.xpath('//*[@class="catText"]/text()').extract():
            print(item)

# --- it runs without project ---

from scrapy.crawler import CrawlerProcess

c = CrawlerProcess({
    'USER_AGENT': 'Mozilla/5.0',
})
c.crawl(MySpider)
c.start()

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2023-03-04
  • 1970-01-01
  • 1970-01-01
  • 2021-03-04
  • 1970-01-01
  • 1970-01-01
  • 2013-05-23
  • 2015-01-12
相关资源
最近更新 更多