【问题标题】:Scrapy plaintext errorScrapy明文错误
【发布时间】:2015-03-29 13:41:55
【问题描述】:

我正在使用 Python Scrapy。我想从没有 HTML 标签的网页中提取文本。下面是我的代码(从这个页面得到想法:How can I get all the plain text from a website with Scrapy?

sel = Selector(response)
        item = DeletespiderItem()
        item['url'] =  response.url
        description = sel.select("//body").extract()
        tree = lxml.html.fromstring(description)
        item['description'] = tree.text_content().strip()
        yield item

但我收到以下错误

File "C:\Python27\lib\site-packages\lxml\html\__init__.py", line 722, in fromstring
        is_full_html = _looks_like_full_html_unicode(html)
    exceptions.TypeError: expected string or buffer

我的代码出了什么问题。我怎样才能得到纯文本?

谁能帮帮我?谢谢,

更新:

Scapy shell https://stackoverflow.com/questions/23156780/how-can-i-get-all-the-plain-text-from-a-website-with-scrapy

sel.select("//body").extract()[0].strip()

o/p \r\n \r\n \r\n \r\n \r\n \r\n聊天\r\n ]

它正在添加额外的\r\n ?

【问题讨论】:

标签: python web-scraping scrapy


【解决方案1】:

extract() 返回一个列表,使用:

description = sel.select("//body").extract()[0]

【讨论】:

  • 它正在工作,但问题是 [\r\n \r\n \r\n \r\n \r\n \r\n chat\r\n ] 它正在添加 Extra转义字符 \r\n ..
  • @Backtrack 你在哪里得到这个值,在哪一步?如果你在这里调用 strip 怎么办:sel.select("//body").extract()[0].strip()?谢谢。
  • 我做了这个但没有工作 [sel.select("//body").extract()[0].strip()] 。不工作
  • @Backtrack 这些换行符不是问题,使用lxml.html 获取您计划的文本。仅供参考,请致电 print(sel.select("//body").extract()[0].strip()) 并查看没有换行符。
  • 当我尝试得到这个错误 ['ascii' codec can't encode character u'\xd7' in position 8468: ordinal not in range(128)]
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-11-19
  • 2018-05-04
  • 2012-06-10
  • 1970-01-01
  • 1970-01-01
  • 2016-04-25
  • 1970-01-01
相关资源
最近更新 更多