我正在尝试刮掉黄页,并在“履带式名称”中出现没有属性响应的错误。我目前正在使用linux,ubuntu和python 2.7.3。代码如下:
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)
答案 0 :(得分:1)
所以最初,我遇到了黄页和数据写入标题表单的问题。绕过这个的最简单方法是将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)]
下一个问题是解析我找到的最好的方法是通过选择器传递响应然后设置选择器(response_通过xpath以下列方式。
def parse_results(self, response):
hxs scrapy.Selector(response)
phone_numbers = hxs.xpath('//p').extract)
for item in phone_numbers:
............
从这里,您只需删除文本并将其写入您需要的任何文档。