Python 连接两个xpath返回
我的问题是如何在打印前连接两个xpath表达式的输出。我需要这个来正确插入MYSQL数据库。 假设我有Python 连接两个xpath返回,python,xpath,Python,Xpath,我的问题是如何在打印前连接两个xpath表达式的输出。我需要这个来正确插入MYSQL数据库。 假设我有 <fieldset class="fieldgroup group-ingred"> <strong><a href="/log/www"><span class="ingredient">Have1</span></a>- </strong>30gramm,<br />
<fieldset class="fieldgroup group-ingred">
<strong><a href="/log/www"><span class="ingredient">Have1</span></a>- </strong>30gramm,<br />
<strong><span class="ingredient">Have2</span> - </strong>50gramm,<br />
<strong>And</strong><span class="ingredient">Have3</span>.<br />
################## there are a lot of similar lines here
</fieldset>
使用此python代码:
for v in doc.xpath("//span[@class='ingredient']/text() | //fieldset[@class='fieldgroup group-ingred']/child::text() "):
v =unicode(v)
print v
但是我想要那样的东西
Have1 30gramm,
Have2 50gramm,
Have3
这样的连接字符串(这只是一个示例)对我没有帮助,因为它输出一个字符串而不是三个
date = ' '.join(td.text for td in doc.xpath(//span[@class='ingredient']/text() | //fieldset[@class='fieldgroup group-ingred']/child::text() ))
print(date)
那么,在将两个xpath表达式的输出打印到for循环之前,如何将其连接起来呢?我将使用。假设您使用for循环对它们进行迭代
>>> import itertools
>>> for i in itertools.zip_longest(range(3), range(15,20)):
print(i)
(0, 15)
(1, 16)
(2, 17)
(None, 18)
(None, 19)
在您的情况下,范围将是xpath查询,如下所示:
doc.xpath("//span[@class='ingredient']/text()"), doc.xpath("//fieldset[@class='fieldgroup group-ingred']/child::text() ")
呃,我对Python不太了解,但这似乎是doc.xpath('//table/tr[@id=“something”]/td')中td的
:print td.text
?你错过了主要问题。问题是如何连接两个xpath查询的结果。是的,我认为这是可行的。它给我打印了相同的符号:u'\u0422,看起来不错,但编码错误。但当我尝试在sql中插入它时,它会输出sqlite3.InterfaceError:Error绑定参数0-可能是不受支持的类型。我不知道,这有什么不对,我不懂Python SQL。我认为您可能无法将元组传递给它。如果这个答案有帮助,那么您可以将其标记为解决方案,这样其他人就不会浪费时间。
doc.xpath("//span[@class='ingredient']/text()"), doc.xpath("//fieldset[@class='fieldgroup group-ingred']/child::text() ")