我正在尝试用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")
答案 0 :(得分: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)
>>>