开发者

Whitespace problem with python and lxml

开发者 https://www.devze.com 2023-02-11 11:40 出处:网络
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.

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,'d开发者_开发技巧etail-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")


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

精彩评论

暂无评论...
验证码 换一张
取 消