【发布时间】:2017-04-10 02:02:46
【问题描述】:
我有以下函数,它接受一个.html 文档并提取一些内容:
for e in soup.tbody.findAll('a', href=True):
tree = etree.fromstring(str(e))
for e in tree.xpath('//b'):
# Here, instead of the above line, I would like to get in a single string all the printed elements in tree.xpath()
如何在一个动作中返回tree.xpath('//a') 中的所有es 列表?我尝试将每个元素和new_lis += element 附加到一个新列表中,但是它不起作用。
【问题讨论】:
-
您能否展示所需输出与当前输出的示例?
-
我用信息更新了问题,谢谢@RobertValencia
-
所以如果你只使用print,它会打印所有的迭代输出,但是如果你将它们附加到列表中,它只会保存第一个迭代输出?
-
这是正确的@RobertValencia,我该怎么做才能获得所需的输出?。
-
在
try语句之前,创建一个字符串s = '',然后将每个新元素添加到s-s += 'www.example.com' + e.get('href')+' | title: ' + e.get('title'),而不是打印。
标签: python python-3.x loops data-structures