Home  >  Article  >  Backend Development  >  javascript - scrpy CrawlSpider自动爬去网页问题

javascript - scrpy CrawlSpider自动爬去网页问题

WBOY
WBOYOriginal
2016-06-06 20:17:131329browse

#我的代码如下,问题在代码的注释里面
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor as sle #用于定义需要提取的链接
class NewsSpider(CrawlSpider):

<code>name = "demo2"
allowed_domains = ["360.cn"]
# start_urls=["http://bobao.360.cn/activity/index&page=2"]#这个连接测试成功
# rules = [ Rule(sle(allow=r'/vul/index?type=all&page=\d{1,3}'), follow=True,callback='parse_item1') ]#*这个测试不成功原因好像*
#allow=r'/vul/index?type=all&page=\d{1,3} 这个里面/vul/index?这个?有影响,请教如何修改
rules = [ Rule(sle(allow=r'/activity/index&page=\d{1,3}'), follow=True,callback='parse_item1') ]#这个连接测试成功
</code>

/activity/index&page=

<code>def parse_item1(self, response):
    print u'这是谁?????????????????????'
    </code>

回复内容:

#我的代码如下,问题在代码的注释里面
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor as sle #用于定义需要提取的链接
class NewsSpider(CrawlSpider):

<code>name = "demo2"
allowed_domains = ["360.cn"]
# start_urls=["http://bobao.360.cn/activity/index&page=2"]#这个连接测试成功
# rules = [ Rule(sle(allow=r'/vul/index?type=all&page=\d{1,3}'), follow=True,callback='parse_item1') ]#*这个测试不成功原因好像*
#allow=r'/vul/index?type=all&page=\d{1,3} 这个里面/vul/index?这个?有影响,请教如何修改
rules = [ Rule(sle(allow=r'/activity/index&page=\d{1,3}'), follow=True,callback='parse_item1') ]#这个连接测试成功
</code>

/activity/index&page=

<code>def parse_item1(self, response):
    print u'这是谁?????????????????????'
    </code>

此处要注意?号的转换,复制过来需要对?号进行转义。

网页中链接是这样:#/rwxwsblog/default.html?page=3"
要写成这样的:Rule(sle(allow=("/rwxwsblog/default.html\?page=\d{1,}")), #此处要注意?号的转换,复制过来需要对?号进行转义。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn