希望你不需要在这里的整套代码,但我有一个问题,我解析HTML,使用XPath,我没有得到我' d预计:XPath不工作,因为我期望它
# here is the current set of tags I'm interested in
html = '''<div style="padding-top: 10px; clear: both; width: 100%;">
<a href="http://www.amazon.com/review/R41M1I2K413NG/ref=cm_aya_cmt?ie=UTF8&ASIN=B013IZY7RU#wasThisHelpful" ><img src="http://g-ecx.images-amazon.com/images/G/01/x-locale/communities/discussion_boards/comment-sm._CB192250344_.gif" width="16" alt="Comment" hspace="3" align="absmiddle" height="16" border="0" /></a> <a href="http://www.amazon.com/review/R41M1I2K413NG/ref=cm_aya_cmt?ie=UTF8&ASIN=B013IZY7RU#wasThisHelpful" >Comment</a> | <a href="http://www.amazon.com/review/R41M1I2K413NG/ref=cm_cr_rdp_perm" >Permalink</a>'''
我试图让第一a
标签,这是一个长的URL的href
值。为此,我使用以下代码
from lxml import etree
import StringIO
parser = etree.HTMLParser(encoding="utf-8")
tree = etree.parse(StringIO.StringIO(html), parser)
style = 'padding-top: 10px; clear: both; width: 100%;'
xpath = "//div[@style='%s']" % style
xpath += "/a[1]/@href"
# use the XPath expression above to pull out the href value
tree.xpath(xpath)
['http://www.amazon.com/review/R41M1I2K413NG/ref=cm_aya_cmt?ie=UTF8&ASIN=B013IZY7RU#wasThisHelpful']
这适用于当我拉出正在使用的零件并将其粘贴为字符串时。这与我使用request.get()
调用构建的tree
完全不一样,我无法弄清楚为什么?它返回的是:
['http://www.amazon.com/review/R41M1I2K413NG]
而我不明白为什么。我明白我在这里黑暗中拍摄,但我只是希望有人遇到了“属性截断的XPath返回值”问题。
编辑:
下面是我目前使用的全部代码,但它不工作。它返回上面的截断值。
from lxml import etree
import requests
import StringIO
from requests.packages.urllib3.util.retry import Retry
from requests.adapters import HTTPAdapter
session = requests.Session()
retries = Retry(total=5, backoff_factor=1, status_forcelist=[502, 503, 504])
session.mount('http://www.amazon.com', HTTPAdapter(max_retries=retries))
parser = etree.HTMLParser(encoding=encoding)
url = "http://www.amazon.com/gp/cdp/member-reviews/ARPJ98Y7U8K5H?ie=UTF8&display=public&page=3&sort_by=MostRecentReview"
page = session.get(url, timeout=5)
tree = etree.parse(StringIO.StringIO(page.text), parser)
style = 'padding-top: 10px; clear: both; width: 100%;'
xpath = "//div[@style='%s']" % style
xpath += "/a[1]/@href"
# use the XPath expression above to pull out the href value
tree.xpath(xpath)
编辑2:
这样确实出于某种原因。而不是创建一个session
对象,并使用提交get
请求,然后传递到parser
,只需将url
字符串传递给parser
作品:
url = "http://www.amazon.com/gp/cdp/member-reviews/ARPJ98Y7U8K5H?ie=UTF8&display=public&page=3&sort_by=MostRecentReview"
tree = etree.parse(url, parser)
for e in tree.xpath("//div[@style='padding-top: 10px; clear: both; width: 100%;']/a[1]/@href"):
print e
据我了解,通过多个网址的循环时会话对象将持续加速进程的连接属性。如果我使用etree.parse(url, parser)
方法,我担心我会失去效率。
我们如何重现这一点?请向我们展示返回截断属性值的确切代码。 – mzjn
调用'request.get()'时,你使用的是什么URL? – Markus
http://www.amazon.com/gp/cdp/member-reviews/ARPJ98Y7U8K5H?ie=UTF8&display=public&page=3&sort_by=MostRecentReview –