gpt4 book ai didi

python - 如何正确使用 PyQuery 遍历?

转载 作者:太空宇宙 更新时间:2023-11-04 02:46:33 25 4
gpt4 key购买 nike

有一个名为“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 class和age class

最后,获取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()

但是现在,我遇到了两个问题

1。我得到的结果不是“Michael:22”,而是“Michael:None”和“None:22”

2。我写入的'name_file'的内容只是'None:None',并不是我得到的所有结果。

最佳答案

第一个问题源于您正在遍历所有 <input ... >元素(由 doc('input') 收集),因此您只能获得姓名或年龄,但不能同时获得两者。您可以做的是遍历个人 <td> ... </td> block 并提取匹配的 child - 有点浪费但要与您的想法保持一致:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com