非常感謝您的回答,John和Steven。你的回答讓我思考不同,這使我找到了問題的根源,也是一個可行的解決方案。
我是用下面的測試代碼的工作:
import urllib
import urllib2
from scrapy.selector import HtmlXPathSelector
from scrapy.http import HtmlResponse
URL = "http://jackjones.bestsellershop.com/DE/jeans/clark-vintage-jos-217-sup/37246/37256"
url_handler = urllib2.build_opener()
urllib2.install_opener(url_handler)
handle = url_handler.open(URL)
response = handle.read()
handle.close()
html_response = HtmlResponse(URL).replace(body=response) # Problematic line
hxs = HtmlXPathSelector(html_response)
desc = hxs.select('//span[@id="attribute-content"]/text()')
desc_text = desc.extract()[0]
print desc_text
print desc_text.encode('utf-8')
的Scrapy外殼內,當我提取的描述數據,就出來罰款。它讓我有理由懷疑我的代碼中出現了問題,因爲在pdb
提示符下,我看到了提取數據中的替換字符。
我通過Scrapy文檔拍得Response class和調整上面這個代碼:
import urllib
import urllib2
from scrapy.selector import HtmlXPathSelector
from scrapy.http import HtmlResponse
URL = "http://jackjones.bestsellershop.com/DE/jeans/clark-vintage-jos-217-sup/37246/37256"
url_handler = urllib2.build_opener()
urllib2.install_opener(url_handler)
handle = url_handler.open(URL)
response = handle.read()
handle.close()
#html_response = HtmlResponse(URL).replace(body=response)
html_response = HtmlResponse(URL, body=response)
hxs = HtmlXPathSelector(html_response)
desc = hxs.select('//span[@id="attribute-content"]/text()')
desc_text = desc.extract()[0]
print desc_text
print desc_text.encode('utf-8')
我所做的更改與html_response = HtmlResponse(URL, body=response)
更換線html_response = HtmlResponse(URL).replace(body=response)
。這是我的理解,replace()
方法是從編碼的角度以某種方式改變特殊字符。
如果有人想要詳細說明replace()
方法做錯了什麼細節,我非常感謝這一努力。
再次感謝您。
非常感謝您的回覆,約翰。我找到了問題並提供瞭解釋它的答案。 – ayaz 2011-04-12 06:53:15