xpath - following-sibling question
basic question that i'm missing something.
have the text html
<tr ><td class="CourseTitle" >foo </td></tr>
<tr "> <th >Code</th><th >Type</th> </tr>
<tr valign="top" bgcolor="#FFFFCC"><td ">40000</td><td ">40000</td></tr>
<tr valign="top" bgcolor="#FFFFCC"><td ">40001</td><td ">40000</td></tr>
<tr valign="top" bgcolor="#FFFFCC"><td ">40002</td><td ">40000</td></tr>
<tr valign="top" bgcolor="#FFFFCC"><td ">40003</td><td ">40000</td></tr>
<tr valign="top" bgcolor="#FFFFCC"><td ">40004</td><td ">40000</td></tr>
<tr class="blue-bar" bgcolor="navy"><td colspan="16"></td></tr>
<tr "><td class="CourseTitle" >asaa </td> </tr>
<tr valign="top" bgcolor="#FFFFCC"><td ">4sd0003</td><td ">40000</td></tr>
<tr valign="top" bgcolor="#FFFFCC"><td ">400sd04</td><td ">40000</td></tr>
i'm trying to figure out a way to get all the subsequent tr/td following the "tr" that has a "td" with a @class="CourseTitle"
something like:
/html/body/div[5]/table/tbody/tr[3]/td[@class="CourseTitle"]/../following-sibling::tr//td[@class="CourseTitle"]
only g开发者_如何学JAVAets a list of all the trs with the matching td/@class which isn'twhat I'm going for.
I've been trying to figure out how you get the siblings, when a child of the sibling matches the condition..
I'm testing this in python, using the libxml libs.
Thanks
I'm not an XPath expert by any mean, but you could always try:
//tr/td[@class="CourseTitle"]/../following-sibling::tr/td[not(@class="CourseTitle")]/..
import lxml.html as lh
import urllib2
url='http://websoc.reg.uci.edu/?YearTerm=2011-92&Breadth=ANY&Dept=CHEM&CourseNum=&Division=ANY&CourseCodes=&InstrName=&CourseTitle=&ClassType=ALL&Units=&Days=&StartTime=&EndTime=&MaxCap=&FullCourses=ANY&FontSize=100&CancelledCourses=Exclude&Bldg=&Room=&Submit=Display+Web+Results'
doc=lh.parse(urllib2.urlopen(url))
# For debugging
# doc=lh.parse('/tmp/Schedule of Classes.html')
for td in doc.xpath('//tr/td[@class="CourseTitle"]'):
title=td.xpath('font/b/text()')[0]
print(title)
keys=td.xpath('../following-sibling::tr/th/text()')
for row in zip(*[iter(td.xpath('../following-sibling::tr/td'))]*len(keys)):
vals=[s.text_content() for s in row]
print(dict(zip(keys,vals)))
yields
GENERAL CHEMISTRY
{'Status': 'OPEN', 'Web': u'\xa0', 'Code': '40060', 'Rstr': 'A', 'Textbooks': 'Bookstore', 'Max': '24', 'WL': 'n/a', 'Enr': '15', 'Sec': 'D10', 'Time': u'F \xa0 12:00-12:50p', 'Units': '0', 'Place': 'RH 108', 'Instructor': 'ARASASINGHAM, R.', 'Type': 'Dis', 'Nor': '0', 'Req': '14'}
...
UNIVERSITY TEACHING
{'Status': 'OPEN', 'Web': u'\xa0', 'Code': '41533', 'Rstr': 'K and S', 'Textbooks': 'Bookstore', 'Max': '150', 'WL': '0', 'Enr': '25', 'Sec': 'A', 'Time': u'M \xa0 12:00-12:50p', 'Units': '1-4', 'Place': 'TBA', 'Instructor': 'TOBIAS, D.BOROVIK, A.', 'Type': 'Tut', 'Nor': '0', 'Req': '26'}
精彩评论