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

标签 python python-2.7 pyquery

有一个名为“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/

相关文章:

python - 如何使用pyquery按文本获取元素?

python - 使用 Python 将整个 JSON 转换为一个 SQLite 字段

python - Python 中最宽容的 HTML 解析器是什么?

python - VScode扩展: CODE RUNNER and PYTHON PREVIEWER do not use the interpreter I select

python - __getattr__ 在实现 __setattr__ 时抛出最大递归错误

python - 使用 %timeit 命令为整个 IPython notebook 计时?

python - 输出给定列表中的所有偶数,每行一个

python - 什么是 C++ 中 "new"的 python 等价物

python - R 脚本无法从 bash 启动直方图

python-2.7 - Flask-Login - 如何获取 session ID