我正在尝试进行递归爬网运行,因为我写的那个没有正常工作,我从网上提取了一个示例并尝试了。我真的不知道,问题在哪里,但爬行不显示任何错误。任何人都可以帮助我。
此外,是否有任何分步调试工具来帮助了解蜘蛛的爬行流程。
非常感谢有关此方面的任何帮助。
MacBook:spiders hadoop$ scrapy crawl craigs -o items.csv -t csv
/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/zope/__init__.py:1: UserWarning: Module pkg_resources was already imported from /System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/pkg_resources.pyc, but /Library/Python/2.6/site-packages is being added to sys.path__import__('pkg_resources').declare_namespace(__name__)
/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/zope/__init__.py:1: UserWarning: Module site was already imported from /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site.pyc, but /Library/Python/2.6/site-packages is being added to sys.path__import__('pkg_resources').declare_namespace(__name__)
2013-02-08 20:35:55+0530 [scrapy] INFO: Scrapy 0.16.4 started (bot: myspider)
2013-02-08 20:35:55+0530 [scrapy] DEBUG: Enabled extensions: FeedExporter, LogStats, TelnetConsole, CloseSpider, WebService, CoreStats, SpiderState
2013-02-08 20:35:55+0530 [scrapy] DEBUG: Enabled downloader middlewares: HttpAuthMiddleware, DownloadTimeoutMiddleware, UserAgentMiddleware, RetryMiddleware, DefaultHeadersMiddleware, RedirectMiddleware, CookiesMiddleware, HttpCompressionMiddleware, ChunkedTransferMiddleware, DownloaderStats
2013-02-08 20:35:55+0530 [scrapy] DEBUG: Enabled spider middlewares: HttpErrorMiddleware, OffsiteMiddleware, RefererMiddleware, UrlLengthMiddleware, DepthMiddleware
2013-02-08 20:35:55+0530 [scrapy] DEBUG: Enabled item pipelines:
2013-02-08 20:35:55+0530 [craigs] INFO: Spider opened
2013-02-08 20:35:55+0530 [craigs] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2013-02-08 20:35:55+0530 [scrapy] DEBUG: Telnet console listening on 0.0.0.0:6023
2013-02-08 20:35:55+0530 [scrapy] DEBUG: Web service listening on 0.0.0.0:6080
2013-02-08 20:35:58+0530 [craigs] DEBUG: Crawled (200) <GET http://sfbay.craigslist.org/npo/> (referer: None)
2013-02-08 20:35:58+0530 [craigs] INFO: Closing spider (finished)
2013-02-08 20:35:58+0530 [craigs] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 230,
'downloader/request_count': 1,
'downloader/request_method_count/GET': 1,
'downloader/response_bytes': 7291,
'downloader/response_count': 1,
'downloader/response_status_count/200': 1,
'finish_reason': 'finished',
'finish_time': datetime.datetime(2013, 2, 8, 15, 5, 58, 415553),
'log_count/DEBUG': 7,
'log_count/INFO': 4,
'response_received_count': 1,
'scheduler/dequeued': 1,
'scheduler/dequeued/memory': 1,
'scheduler/enqueued': 1,
'scheduler/enqueued/memory': 1,
'start_time': datetime.datetime(2013, 2, 8, 15, 5, 55, 343482)}
2013-02-08 20:35:58+0530 [craigs] INFO: Spider closed (finished)
我使用的代码如下,
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.selector import HtmlXPathSelector
#from craigslist_sample.items import CraigslistSampleItem
class MySpider(CrawlSpider):
name = "craigs"
allowed_domains = ["sfbay.craigslist.org"]
start_urls = ["sfbay.craigslist.org/npo/"]
rules = (Rule (SgmlLinkExtractor(allow=("d00\.html", ),restrict_xpaths=('//p[@id="nextpage"]',))
, callback="parse_items", follow= True),
)
def parse_items(self, response):
hxs = HtmlXPathSelector(response)
titles = hxs.select("//p")
items = []
for titles in titles:
item = CraigslistSampleItem()
item ["title"] = titles.select("a/text()").extract()
item ["link"] = titles.select("a/@href").extract()
items.append(item)
return(items)
答案 0 :(得分:1)
尝试使用"d00\.html"
或".*00\.html"
"index\d+00\.html"
答案 1 :(得分:1)
restrict_xpaths
部分这些更改将解决遇到的问题。我还会对用于选择标题的xpath提出以下建议,因为这将删除将出现的空项,因为还会选择下一页链接。
def parse_items(self, response):
hxs = HtmlXPathSelector(response)
titles = hxs.select("//p[@class='row']")