【发布时间】:2014-09-02 20:27:32
【问题描述】:
我正在尝试编写一个爬取域上所有结果页面的 Scrapy 蜘蛛:https://www.ghcjobs.apply2jobs.com...。代码应该做三件事:
(1) 浏览 1-1000 的所有页面。这些页面是相同的,除了 URL 的最后部分不同:&CurrentPage=#。
(2) 访问包含职位发布的结果表中的每个链接,其中链接的 class= SearchResult。这些是表中唯一的链接,所以我在这里没有任何问题。
(3) 将职位描述页面上显示的信息以 key:value JSON 格式存储。 (这部分工作,以基本的方式)
我以前使用过scrapy和CrawlSpiders,使用'rule = [Rule(LinkExtractor(allow='方法递归解析页面以查找与给定正则表达式模式匹配的所有链接。我目前在第1步上感到困惑,爬过上千个结果页面。
下面是我的蜘蛛代码:
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.http.request import Request
from scrapy.contrib.linkextractors import LinkExtractor
from genesisSpider.items import GenesisJob
class genesis_crawl_spider(CrawlSpider):
name = "genesis"
#allowed_domains = ['http://www.ghcjobs.apply2jobs.com']
start_urls = ['https://www.ghcjobs.apply2jobs.com/ProfExt/index.cfm?fuseaction=mExternal.returnToResults&CurrentPage=1']
#allow &CurrentPage= up to 1000, currently ~ 512
rules = [Rule(LinkExtractor(allow=("^https://www.ghcjobs.apply2jobs.com/ProfExt/
index.cfm\?fuseaction=mExternal.returnToResults&CurrentPage=[1-1000]$")), 'parse_inner_page')]
def parse_inner_page(self, response):
self.log('===========Entrered Inner Page============')
self.log(response.url)
item = GenesisJob()
item['url'] = response.url
yield item
这是蜘蛛的输出,顶部有一些执行代码被截断:
2014-09-02 16:02:48-0400 [genesis] DEBUG: Crawled (200) <GET https://www.ghcjobs
.apply2jobs.com/ProfExt/index.cfm?fuseaction=mExternal.returnToResults&CurrentPa
ge=1> (referer: None) ['partial']
2014-09-02 16:02:48-0400 [genesis] DEBUG: Crawled (200) <GET https://www.ghcjobs
.apply2jobs.com/ProfExt/index.cfm?CurrentPage=1&fuseaction=mExternal.returnToRes
ults> (referer: https://www.ghcjobs.apply2jobs.com/ProfExt/index.cfm?fuseaction=
mExternal.returnToResults&CurrentPage=1) ['partial']
2014-09-02 16:02:48-0400 [genesis] DEBUG: ===========Entrered Inner Page========
====
2014-09-02 16:02:48-0400 [genesis] DEBUG: https://www.ghcjobs.apply2jobs.com/Pro
fExt/index.cfm?CurrentPage=1&fuseaction=mExternal.returnToResults
2014-09-02 16:02:48-0400 [genesis] DEBUG: Scraped from <200 https://www.ghcjobs.
apply2jobs.com/ProfExt/index.cfm?CurrentPage=1&fuseaction=mExternal.returnToResu
lts>
{'url': 'https://www.ghcjobs.apply2jobs.com/ProfExt/index.cfm?CurrentPag
e=1&fuseaction=mExternal.returnToResults'}
2014-09-02 16:02:48-0400 [genesis] INFO: Closing spider (finished)
2014-09-02 16:02:48-0400 [genesis] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 930,
'downloader/request_count': 2,
'downloader/request_method_count/GET': 2,
'downloader/response_bytes': 92680,
'downloader/response_count': 2,
'downloader/response_status_count/200': 2,
'finish_reason': 'finished',
'finish_time': datetime.datetime(2014, 9, 2, 20, 2, 48, 611000),
'item_scraped_count': 1,
'log_count/DEBUG': 7,
'log_count/INFO': 7,
'request_depth_max': 1,
'response_received_count': 2,
'scheduler/dequeued': 2,
'scheduler/dequeued/memory': 2,
'scheduler/enqueued': 2,
'scheduler/enqueued/memory': 2,
'start_time': datetime.datetime(2014, 9, 2, 20, 2, 48, 67000)}
2014-09-02 16:02:48-0400 [genesis] INFO: Spider closed (finished)
目前,我坚持这个项目的目标 (1)。如您所见,我的蜘蛛只爬过 start_url 页面。我的正则表达式应该正确定位页面导航按钮,因为我已经测试了正则表达式。我的回调函数 parse_inner_page 正在工作,正如我插入的调试注释所示,但仅在第一页上。我是否错误地使用了“规则”?我在想也许是 HTTPS 的页面应该归咎于......
作为修补解决方案的一种方式,我尝试对第二页结果使用手动请求;这没有用。这也是它的代码。
Request("https://www.ghcjobs.apply2jobs.com/ProfExt/index.cfm?fuseaction=mExternal.returnToResults&CurrentPage=2", callback = 'parse_inner_page')
任何人都可以提供任何指导吗?有没有更好的方法来做到这一点?自周五以来,我一直在研究 SO / Scrapy 文档。非常感谢。
更新:我已经解决了这个问题。问题出在我使用的起始网址上。
start_urls = ['https://www.ghcjobs.apply2jobs.com/ProfExt/index.cfm?fuseaction=mExternal.returnToResults&CurrentPage=1']
通过点击This 页面上的“搜索”按钮,进入表单提交后页面。这会在客户端运行 javascript 以向服务器提交表单,服务器会报告完整的工作板,第 1-512 页。但是,存在另一个硬编码的 URL,它显然调用服务器而不需要使用任何客户端 javascript。所以现在我的起始网址是
start_urls = ['https://www.ghcjobs.apply2jobs.com/ProfExt/index.cfm?fuseaction=mExternal.searchJobs']
一切都回到了正轨!以后看看有没有javascript独立的url来调用服务器资源。
【问题讨论】:
-
正则表达式的 [1-1000] 部分应该是 \d+ ,我认为您需要将 follow=True 添加到您的规则中
-
他们网站的一个问题是结果页面无限期地向上计数,这就是为什么我使用 [1-1000] 而不是 \d+,“任意数字”方法的原因。我之前在规则中遵循=True 无济于事......谢谢。
-
您要从这些页面中删除什么...我没有看到任何内容:没有符合您条件的结果。 ??没有什么可刮的!
标签: python regex scrapy web-crawler