作者stevec (steve)
看板Python
标题scrapy xpath extraction 以及其编码的问题
时间Sat Nov 29 19:20:32 2014
有点不晓得为什麽,想请各位大大看一下
下面的程式码只要是想利用scrapy 里面的xpath extract一些我想要的info
raw_html_article_content_ 是储存我想extract的部分资讯
raw 是储存范围比较大的部分
所以理论上raw会包含raw_html_article_content_ 的资讯
可是raw包含的部分会有点跟raw_html_article_content_里面的不一样
例如:
raw: 结婚并无Z>B (这跟chrom浏览器打开source code的看到的是一样的)
raw_html_article_content_ : 结婚并无Z>B
我要怎麽让raw里面储存的跟raw_html_article_content_的一样啊?
ps:环境win 7, python 2.7,scrappy 1.4
from scrapy.http import HtmlResponse
from scrapy.selector import Selector
import urllib
import urllib2
address = "
http://webptt.com/cn.aspx?n=bbs/Boy-Girl/M.1416362560.A.881.html"
response = urllib2.urlopen(address)
html = response.read()
html_response = HtmlResponse( address, body=html)
sel = Selector(html_response)
recog_assist_word = u"※ 文章网址: "
xpath = """/html/body/div[@id="main-container"]/div[@id="main-content"]/
span[@class="f2" and text()="%s"][last()]/preceding-sibling::node()"""
% recog_assist_word
raw_html_article_content_ = sel.xpath(xpath).extract()
raw_html_article_content_ = "".join([_ for _ in raw_html_article_content_])
raw=sel.xpath(u"""/html""").extract()[0]
print raw_html_article_content_
print raw
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 140.112.218.124
※ 文章网址: http://webptt.com/cn.aspx?n=bbs/Python/M.1417260034.A.579.html
※ 编辑: stevec (140.112.218.124), 11/29/2014 19:39:32
1F:→ dritchie: 那个编码叫HTML entity 11/30 01:27
2F:→ stevec: 感谢大大,可是在python里要怎麽样让name entities显示正常 11/30 11:03
3F:→ stevec: 呢? 11/30 11:04
4F:→ stevec: 为什麽scrapy有时候会帮忙修正,有时候又不会呢? 11/30 11:05
5F:→ stevec: 这个眉角在哪啊? 11/30 11:05