一无所有

时间:2019-07-08 12:32:22

标签: python scrapy robots.txt

作为我对python的研究的一部分,我尝试取消mail.ru主页 新闻专栏

我已允许爬网,我添加了自定义用户代理。我的xpath位置不同,什么也没得到,只是空列表。

import scrapy

class TestmailspidetSpider(scrapy.Spider):
    name = 'testmailspidet'
    allowed_domains = ['mail.ru']
    start_urls = ['http://mail.ru/']

    def parse(self, response):

    yield {
        'testing':response.xpath('//span[@class="i-link-deco i-inline"][position()=1]').extract_first()
    }

1 个答案:

答案 0 :(得分:1)

robots.txt(https://mail.ru/robots.txt)禁止使用。如果仍要抓取,则必须将ROBOTSTXT_OBEY设置为False。您可以包括以下内容:

custom_settings = {
    'ROBOTSTXT_OBEY': False,
}

此外,xpath没有给出任何结果-可能是因为内容是动态加载的。您可以使用scrapy shell检查一下scrapy看到的html页面,如下所示:scrapy shell -s ROBOTSTXT_OBEY=False "http://mail.ru/"。 然后,可以按以下方式构造获取标题的xpath://*[@id="news:main:list"]//*[@class="news__list__item__link__text"]/text()