[英]Recursively Scraping Web Pages With Scrapy
“ http://www.example.com/listing.php?num=2& ”
这是我的蜘蛛的代码,它在单个页面上显示链接列表:
from scrapy.log import *
from crawler_bhinneka.settings import *
from crawler_bhinneka.items import *
import pprint
from MySQLdb import escape_string
import urlparse
def complete_url(string):
"""Return complete url"""
return "http://www.example.com" + string
class BhinnekaSpider(CrawlSpider):
name = 'bhinneka_spider'
start_urls = [
'http://www.example.com/listing.php?'
]
def parse(self, response):
hxs = HtmlXPathSelector(response)
# HXS to find url that goes to detail page
items = hxs.select('//td[@class="lcbrand"]/a/@href')
for item in items:
link = item.extract()
print("my Url Link : ",complete_url(link))
知道我可以在我的第一页中获得“所有”链接。
我想用递归规则使该蜘蛛程序跟随下一页的链接吗?您知道如何在蜘蛛程序中尝试我的规则以获取下一页的链接值。
编辑
@Toan,谢谢您的答复。 我试图使您发送给我的本教程链接成为一个链接,但我只是采用一页(第一页)的项目值。
我在以下URL上查看了源代码:“ http://sfbay.craigslist.org/npo/ ”,但我看不到此strict_xpaths中匹配的xpath值(在代码源中不存在“ class =“ nextpage doies” )
这是您的链接示例的规则:
rules = (Rule (SgmlLinkExtractor (allow = ("index \ d00 \. html") restrict_xpaths = ('/ / p [@ class = "nextpage"]'))
, Callback = "parse_items" follow = True)
)
Scrapy linkextractors用于提取网页的链接。
这是一个示例: http : //mherman.org/blog/2012/11/08/recursively-scraping-web-pages-with-scrapy/#.U9Dl8h_FsUQ
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.