【问题标题】:Whitespace problem with python and lxmlpython和lxml的空格问题
【发布时间】:2011-02-14 13:53:08
【问题描述】:

我正在尝试用 python 和 lxml 抓取这个网站。 它在我的本地机器上工作得很好,但是当我尝试在我的服务器上部署它并运行脚本时,我遇到了空格问题。 网址:http://www.samsungapps.com/topApps/topAppsDetail.as?productId=G00000467050&listYN=Y

html中的问题部分:

<tr>
                            <th>Version</th>
                            <td>

                                1.0.0 (14.02.2011)


                            </td>
                        </tr>

我认为这是我的服务器配置的问题。 有人知道我错过了什么吗?

提前致谢

编辑:

html = seite.read()
    seite.close()
    tree = etree.parse(StringIO.StringIO(html), parser)
    xpath = "//div[contains(@class,'detail-view')]/h4/text()"
    name = tree.xpath(xpath)
    xpath = "//div[contains(@class,'detail-view')]/table/tbody/tr[2]/td/text()"
    cat = tree.xpath(xpath)
    xpath = "//div[contains(@class,'detail-view')]/table/tbody/tr[3]/td/text()"
    typ = tree.xpath(xpath)
    xpath = "//div[contains(@class,'detail-view')]/table/tbody/tr[4]/td/text()"
    version = tree.xpath(xpath)

print name[0].encode("utf-8")
    print cat[0].encode("utf-8")
    print typ[0].encode("utf-8")
    print version[0].encode("utf-8")

【问题讨论】:

  • 您能否提供有关错误的更多详细信息?
  • 这本身不是错误。它只是将空格导出到我的字符串。在我的本地机器上,它会自动去除空格
  • 如果您包含相关的代码片段会很有帮助。
  • 如果元素值中的空格是问题所在,为什么不直接调用 .strip() 呢?
  • 添加了一些代码。正如我在一台机器上提到的,标签中的空格会被自动去除,而在另一台机器(相同的代码)上,空格不会被去除。在机器 1 (Windows XP) 上再次测试它 - 工作和机器 2 (Windows 7) - 空白问题

标签: python lxml


【解决方案1】:

添加.strip() 对我有用

import urllib2,StringIO
from lxml import etree

seite = urllib2.urlopen("http://www.samsungapps.com/topApps/topAppsDetail.as?productId=G00000467050&listYN=Y")
html = seite.read()
seite.close()
parser = etree.HTMLParser()
tree = etree.parse(StringIO.StringIO(html), parser)
xpath = "//div[contains(@class,'detail-view')]/h4/text()"
name = tree.xpath(xpath)
xpath = "//div[contains(@class,'detail-view')]/table/tbody/tr[2]/td/text()"
cat = tree.xpath(xpath)
xpath = "//div[contains(@class,'detail-view')]/table/tbody/tr[3]/td/text()"
typ = tree.xpath(xpath)
xpath = "//div[contains(@class,'detail-view')]/table/tbody/tr[4]/td/text()"
version = tree.xpath(xpath)

print name[0].strip().encode("utf-8")
print cat[0].strip().encode("utf-8")
print typ[0].strip().encode("utf-8")
print version[0].strip().encode("utf-8")

测试

$ /usr/bin/python
Python 2.6.6 (r266:84292, Sep 15 2010, 16:22:56) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib2,StringIO
>>> from lxml import etree
>>> 
>>> seite = urllib2.urlopen("http://www.samsungapps.com/topApps/topAppsDetail.as?productId=G00000467050&listYN=Y")
>>> html = seite.read()
>>> seite.close()
>>> parser = etree.HTMLParser()
>>> tree = etree.parse(StringIO.StringIO(html), parser)
>>> xpath = "//div[contains(@class,'detail-view')]/h4/text()"
>>> name = tree.xpath(xpath)
>>> xpath = "//div[contains(@class,'detail-view')]/table/tbody/tr[2]/td/text()"
>>> cat = tree.xpath(xpath)
>>> xpath = "//div[contains(@class,'detail-view')]/table/tbody/tr[3]/td/text()"
>>> typ = tree.xpath(xpath)
>>> xpath = "//div[contains(@class,'detail-view')]/table/tbody/tr[4]/td/text()"
>>> version = tree.xpath(xpath)
>>> 
>>> print name[0].strip().encode("utf-8")
Dark
>>> print cat[0].strip().encode("utf-8")
Theme
>>> print typ[0].strip().encode("utf-8")
Application
>>> print version[0].strip().encode("utf-8")
1.0.0 (14.02.2011)
>>> 

【讨论】:

  • @ibu2002,它会返回大量没有 strip() 的制表符和空格。
  • 好吧,这很有趣。在我左边的机器上,我可以在没有 strip() 的情况下执行代码并且没有空格......知道为什么吗?
  • @suddenbreak Ubuntu 16 不会删除换行符。 Ubuntu 18 可以。仍然不知道为什么。
猜你喜欢
  • 2021-04-23
  • 2011-07-17
  • 1970-01-01
  • 1970-01-01
  • 2019-04-15
  • 2017-01-04
  • 1970-01-01
  • 2012-06-13
  • 1970-01-01
相关资源
最近更新 更多