我有一个scrapy代码,应该可以从网络中的表格中接听电话和地址:
import scrapy
class PeopleSpider(scrapy.Spider):
name="People"
start_urls=[
'http://canada411.yellowpages.ca/search/si/1/519-896-7080/',
]
def parse(self,response):
for people in response.css("div.person-search__table--row"):
yield {
'Name': people.css('div.person-search__table--name::text').extract_first(),
'Phone Number': people.css('div.person-search__table--phoneNumber::text').extract_first(),
'Street': people.css('div.person-search__table--name::street').extract_first(),
'City': people.css('div.person-search__table--city::text').extract_first(),
'Province': people.css('div.person-search__table--province::text').extract_first(),
'Postal Code': people.css('div.person-search__table--postalCode::text').extract_first(),
}
但我一直得到0个抓取的页面......
scrapy runspider get.py -o people.json
2017-02-15 20:14:26 [scrapy.utils.log] INFO: Scrapy 1.3.2 started (bot: scrapybot)
2017-02-15 20:14:26 [scrapy.utils.log] INFO: Overridden settings: {'FEED_FORMAT': 'json', 'FEED_URI': 'people.json'}
2017-02-15 20:14:26 [scrapy.middleware] INFO: Enabled extensions:
['scrapy.extensions.feedexport.FeedExporter',
'scrapy.extensions.logstats.LogStats',
'scrapy.extensions.telnet.TelnetConsole',
'scrapy.extensions.corestats.CoreStats']
2017-02-15 20:14:26 [scrapy.middleware] INFO: Enabled downloader middlewares:
['scrapy.downloadermiddlewares.httpauth.HttpAuthMiddleware',
'scrapy.downloadermiddlewares.downloadtimeout.DownloadTimeoutMiddleware',
'scrapy.downloadermiddlewares.defaultheaders.DefaultHeadersMiddleware',
'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware',
'scrapy.downloadermiddlewares.retry.RetryMiddleware',
'scrapy.downloadermiddlewares.redirect.MetaRefreshMiddleware',
'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware',
'scrapy.downloadermiddlewares.redirect.RedirectMiddleware',
'scrapy.downloadermiddlewares.cookies.CookiesMiddleware',
'scrapy.downloadermiddlewares.stats.DownloaderStats']
2017-02-15 20:14:26 [scrapy.middleware] INFO: Enabled spider middlewares:
['scrapy.spidermiddlewares.httperror.HttpErrorMiddleware',
'scrapy.spidermiddlewares.offsite.OffsiteMiddleware',
'scrapy.spidermiddlewares.referer.RefererMiddleware',
'scrapy.spidermiddlewares.urllength.UrlLengthMiddleware',
'scrapy.spidermiddlewares.depth.DepthMiddleware']
2017-02-15 20:14:26 [scrapy.middleware] INFO: Enabled item pipelines:
[]
2017-02-15 20:14:26 [scrapy.core.engine] INFO: Spider opened
2017-02-15 20:14:26 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2017-02-15 20:14:26 [scrapy.extensions.telnet] DEBUG: Telnet console listening on 127.0.0.1:6023
2017-02-15 20:14:27 [scrapy.downloadermiddlewares.redirect] DEBUG: Redirecting (302) to <GET http://99.227.194.212/> from <GET http://canada411.yellowpages.ca/search/si/1/519-896-7080/>
2017-02-15 20:14:27 [scrapy.downloadermiddlewares.redirect] DEBUG: Redirecting (302) to <GET http://99.227.194.212/login.html> from <GET http://99.227.194.212/>
2017-02-15 20:14:27 [scrapy.core.engine] DEBUG: Crawled (200) <GET http://99.227.194.212/login.html> (referer: None)
2017-02-15 20:14:27 [scrapy.core.engine] INFO: Closing spider (finished)
2017-02-15 20:14:27 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 681,
'downloader/request_count': 3,
'downloader/request_method_count/GET': 3,
'downloader/response_bytes': 7931,
'downloader/response_count': 3,
'downloader/response_status_count/200': 1,
'downloader/response_status_count/302': 2,
'finish_reason': 'finished',
'finish_time': datetime.datetime(2017, 2, 16, 1, 14, 27, 273208),
'log_count/DEBUG': 4,
'log_count/INFO': 7,
'response_received_count': 1,
'scheduler/dequeued': 3,
'scheduler/dequeued/memory': 3,
'scheduler/enqueued': 3,
'scheduler/enqueued/memory': 3,
'start_time': datetime.datetime(2017, 2, 16, 1, 14, 26, 788223)}
2017-02-15 20:14:27 [scrapy.core.engine] INFO: Spider closed (finished)
我的代码有问题吗?或者该网址无法解析?
答案 0 :(得分:0)
当您尝试抓取网址时,您会收到Redirecting (302)
消息,这意味着您将从您尝试抓取的网页发送出去。您需要添加元值才能停止重定向;请参阅the answer here了解可能的解决方案。
答案 1 :(得分:0)
你正在抓Yelopages网站并且不容易抓它们。
我确信他们正在重定向到验证码页面。我过去曾抓过那个网站。
您可以尝试使用这段代码来查看重定向到哪个页面。
from scrapy.utils.response import open_in_browser
def parse_details(self, response):
open_in_browser(response)
它将在浏览器中打开已删除的URL(如果您在Ubuntu,Windows或Mac上运行此scrapy项目)