2011-02-14 67 views
1

我試圖用python和lxml來抓取這個網站。 它的工作在我的本地機器上運行,但是當我嘗試將它部署在我的服務器上並運行腳本時,我遇到了空白問題。 網址:在HTML http://www.samsungapps.com/topApps/topAppsDetail.as?productId=G00000467050&listYN=Ypython和lxml的空白問題

問題的一部分:

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

你能給出有關錯誤的更多細節? – yanjost 2011-02-14 14:10:39

+0

它本身並不是一個錯誤。 它只是將空格輸出到我的字符串中。 在我的本地機器上它自動剝離空白 – suddenbreak 2011-02-14 14:32:33

回答

0

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