有一个名为“name.txt”的文件
内容如下
<td>
<input class="name" value="Michael">
<input class="age" value="22">
<input class="location" value="hebei">
</td>
<td>
<input class="name" value="Jack">
<input class="age" value="23">
<input class="location" value="NewYo">
</td>
现在我想使用pyquery获取所有输入标签,然后遍历输入标签
使用“.filter”获取所有姓名类别和年龄类别
最后,获取name和age的值并将所有结果写入名为'name_file.txt'的文件中
我的代码如下
# -*- coding: utf-8 -*-
from pyquery import PyQuery as pq
doc = pq(filename='name.txt')
input = doc('input')
for result in input.items():
name_result = result.filter('.name')
age_result = result.filter('.age')
name = name_result.attr('value')
age = age_result.attr('value')
print "%s:%s" %(name,age)
c = "%s:%s" %(name,age)
f = file('name_file.txt','w')
f.write(c)
f.close()
但是现在,我遇到了 2 个问题
1。我得到的结果不是“Michael:22”,而是“Michael:None”和“None:22”
2。我写入的“name_file”内容只是“None:None”,而不是我得到的所有结果。
最佳答案
第一个问题源于这样一个事实:您正在循环遍历所有 <input ... >
元素(由 doc('input')
收集),因此您只能获取姓名或年龄,但不能获取两者。你能做的就是循环遍历个人 <td> ... </td>
block 并提取匹配的子项 - 有点浪费,但与您的想法保持一致:
from pyquery import PyQuery as pq
doc = pq(filename='name.txt') # open our document from `name.txt` file
for result in doc('td').items(): # loop through all <td> ... </td> items
name_result = result.find('.name') # grab a tag with class="name"
age_result = result.find('.age') # grab a tag with class="age"
name = name_result.attr('value') # get the name's `value` attribute value
age = age_result.attr('value') # get the age's `value` attribute value
print("{}:{}".format(name, age)) # print it to the STDOUT as name:age
至于第二部分 - 您正在打开 name_file.txt
在写入模式下打开文件,写入一行,然后在每个循环中关闭它 - 当您在写入模式下打开文件时,它将截断其中的所有内容,以便您继续为每个循环写入第一行。尝试这样做:
from pyquery import PyQuery as pq
doc = pq(filename='name.txt') # open our document from `name.txt` file
with open("name_file.txt", "w") as f: # open name_file.txt for writing
for result in doc('td').items(): # loop through all <td> ... </td> items
name_result = result.find('.name') # grab a tag with class="name"
age_result = result.find('.age') # grab a tag with class="age"
name = name_result.attr('value') # get the name's `value` attribute value
age = age_result.attr('value') # get the age's `value` attribute value
print("{}:{}".format(name, age)) # print values to the STDOUT as name:age
f.write("{}:{}\n".format(name, age)) # write to the file as name:age + a new line
关于python - 如何正确使用 PyQuery 遍历?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44982257/