python 和 lxml 的空格问题

发布于 2024-10-17 11:13:07 字数 1315 浏览 2 评论 0原文

我正在尝试使用 python 和 lxml 抓取这个网站。 它在我的本地计算机上工作得很好,但是当我尝试将它部署在我的服务器上并运行脚本时,我遇到了空格问题。 URL: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")

I'm trying do scrape this website with python and lxml.
Its working greate on my local machine, but when i try to deploy it on my server and run the script i got problems with whitespaces.
URL: http://www.samsungapps.com/topApps/topAppsDetail.as?productId=G00000467050&listYN=Y

Problem part in html:

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

                                1.0.0 (14.02.2011)


                            </td>
                        </tr>

I think it's a problem of configuration of my server.
Does anyone got an idea what i missed?

Thanks in advance

Edit:

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")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九歌凝 2024-10-24 11:13:08

添加 .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)
>>> 

Adding .strip() works for me

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")

Tests

$ /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)
>>> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文