【问题标题】:Confusion on Scrapy re-direct behavior?对 Scrapy 重定向行为感到困惑?
【发布时间】:2017-09-23 00:25:45
【问题描述】:

所以我试图从具有无限滚动类型布局的新闻网站上抓取文章,所以会发生以下情况:

example.com有第一页文章

example.com/page/2/有第二页

example.com/page/3/ 有第三页

等等。当您向下滚动时,网址会发生变化。考虑到这一点,我想抓取第一个 x 文章数量并执行以下操作:

start_urls = ['http://example.com/']
for x in range(1,x):
    new_url  = 'http://www.example.com/page/' + str(x) +'/'
    start_urls.append(new_url)

前 9 页似乎工作正常,我得到如下内容:

Redirecting (301) to <GET http://example.com/page/4/> from <GET http://www.example.com/page/4/>
Redirecting (301) to <GET http://example.com/page/5/> from <GET http://www.example.com/page/5/>
Redirecting (301) to <GET http://example.com/page/6/> from <GET http://www.example.com/page/6/>
Redirecting (301) to <GET http://example.com/page/7/> from <GET http://www.example.com/page/7/>
2017-09-08 17:36:23 [scrapy.extensions.logstats] INFO: Crawled 3 pages (at 3 pages/min), scraped 0 items (at 0 items/min)
Redirecting (301) to <GET http://example.com/page/8/> from <GET http://www.example.com/page/8/>
Redirecting (301) to <GET http://example.com/page/9/> from <GET http://www.example.com/page/9/>
Redirecting (301) to <GET http://www.example.com/> from <GET http://www.example.com/page/10/>
Redirecting (301) to <GET http://www.example.com/> from <GET http://www.example.com/page/11/>
Redirecting (301) to <GET http://www.example.com/> from <GET http://www.example.com/page/12/>
Redirecting (301) to <GET http://www.example.com/> from <GET http://www.example.com/page/13/>

从第 10 页开始,它从 example.com/page/10/ 重定向到类似 example.com/ 的页面,而不是原始链接 example.com/page/10。什么可能导致这种行为?

我研究了几个选项,例如 dont_redirect,但我就是不明白发生了什么。这种重定向行为的原因是什么?尤其是当您直接输入网站的链接(如example.com/page/10?)时,不会发生重定向。

任何帮助将不胜感激,谢谢!

[编辑]

class spider(CrawlSpider):
    start_urls = ['http://example.com/']

    for x in range(startPage,endPage):
        new_url  = 'http://www.example.com/page/' + str(x) +'/'
        start_urls.append(new_url)
   custom_settings = {'DEPTH_PRIORITY': 1, 'DEPTH_LIMIT': 1}


rules = (
    Rule(LinkExtractor(allow=('some regex here,')deny=('example\.com/page/.*','some other regex',),callback='parse_article'),
)

def parse_article(self, response):
    #some parsing work here 
    yield item

是因为我在LinkExtractor 中包含了example\.com/page/.*?但是,这不应该只适用于不是start_url 的链接吗?

【问题讨论】:

  • 您是否因为该页面不存在而被重定向?您要抓取哪个网站?
  • 你能发布一个你的实际代码的最小例子吗?
  • @Bricky 我无法发布详细信息,但我已更新问题以包含任何相关内容,谢谢!

标签: python web-scraping scrapy


【解决方案1】:

看起来此站点使用某种安全措施仅检查请求标头中的 User-Agent

所以你只需要在settings.py文件中添加一个普通的User-Agent即可:

USER_AGENT = 'Mozilla/5.0'

另外,蜘蛛不一定需要start_urls属性来获取起始站点,你也可以使用start_requests方法,所以将所有start_urls的创建替换为:

class spider(CrawlSpider):

    ...

    def start_requests(self):
        for x in range(1,20):
            yield Request('http://www.example.com/page/' + str(x) +'/')

    ...

【讨论】:

  • 谢谢!你是说因为它正在重定向的用户代理吗?为什么我的用户代理对于所有请求都不相同?当您直接使用Request() 发出请求时,请求不会被重定向吗?抱歉,只是想了解更多,谢谢!
  • settings.py 中的USER_AGENT 被蜘蛛中执行的所有请求使用
  • 谢谢,澄清一下,我问的是为什么上面的更改会在蜘蛛发出请求时停止example.com/page/10/ 的重定向--> example.com 如果这有意义的话。
  • 你试过我的建议了吗?对于您未编写代码(但其他程序员)的网站如何工作没有解释,您只需要找到如何使其工作,并使用用户代理的请求解决这种情况。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-21
  • 1970-01-01
相关资源
最近更新 更多