繁体   English   中英

如何使用Scrapy爬行黄页

[英]How to crawl Yellowpages with Scrapy

我正在尝试刮除黄页,并出现一个错误,该错误是“搜寻器名称”中没有属性响应。 我目前正在使用python 2.7.3在linux,ubuntu中工作。 代码如下:

import scrapy
from scrapy.spider import Spider
from scrapy.http import FormRequest, Request
from scrapy.selector import HtmlXPathSelector

class yellowpages(scrapy.Spider):
    name = 'yellowpages'
    allowed_domains = ['yellowpages.com']
    start_urls = ['http://www.yellowpages.com/whitepages']

def parse(self, response):
  return [FormRequest.from_response(response, headers = None, formdata = {"last name": "lastname person one"}, callback = self.parse_results)]

def parse_results(self, response):
    hxs = HtmlXPathSelector(response)
    print hxs.select('//h3').extract()

编辑:

根据要求,我把输出。 由于某种原因,现在似乎正在输出页面。 我认为即使保存它,它可能也一直在运行代码,因此我需要重新启动终端。 现在似乎正在发布一些CSS。

     ScrapyDeprecationWarning: Call to deprecated function select. Use .xpath() instead.
  print hxs.select('//h3').extract()
/usr/local/lib/python2.7/dist-packages/scrapy/selector/unified.py:106:       ScrapyDeprecationWarning: scrapy.selector.HtmlXPathSelector is deprecated, instantiate scrapy.Selector instead.
  for x in result]
#CSS Output, removed between terminal and stackoverflow to help with formationg
2014-07-06 15:22:16-0400 [yellowpages] INFO: Closing spider (finished)
2014-07-06 15:22:16-0400 [yellowpages] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 2382,
 'downloader/request_count': 3,
 'downloader/request_method_count/GET': 3,
 'downloader/response_bytes': 40878,
 'downloader/response_count': 3,
 'downloader/response_status_count/200': 2,
 'downloader/response_status_count/301': 1,
 'finish_reason': 'finished',
 'finish_time': datetime.datetime(2014, 7, 6, 19, 22, 16, 316969),
 'log_count/DEBUG': 5,
 'log_count/INFO': 7,
 'request_depth_max': 1,
 'response_received_count': 2,
 'scheduler/dequeued': 3,
 'scheduler/dequeued/memory': 3,
 'scheduler/enqueued': 3,
 'scheduler/enqueued/memory': 3,
 'start_time': datetime.datetime(2014, 7, 6, 19, 22, 14, 117396)}
2014-07-06 15:22:16-0400 [yellowpages] INFO: Spider closed (finished)

因此最初,我在将黄页和数据写入页眉形式时遇到问题。 绕过此问题的最简单方法是将form_number设置为1。代码如下:

def parse(self, response):
    return [FormRequest.from_response(response, header = None, formnumber = 1, formdata = {"last": "examplename", "state": "examplestate"}, cakkback = self.parse_results)]

下一个问题是解析我发现的最佳方法是通过选择器传递响应,然后以以下方式通过xpath设置选择器(response_)。

def parse_results(self, response):
    hxs scrapy.Selector(response)
    phone_numbers = hxs.xpath('//p').extract)
    for item in phone_numbers:
            ............

从这里,您只需删除文本并将其写入所需的任何文档即可。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM