There is a file called "name.txt"
Content is below
<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>
Now I want to use pyquery to get all input tags, then traversal input tags
Use '.filter' to get all name class and age class
At last, get the value of name and age and write all results into a file called'name_file.txt'
My code is below
# -*- 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()