BryceSoker
BryceSoker

Reputation: 644

Python xpath to get text from a table

So with request and lxml I have been trying to create a small API that given certain parameters would download a timetable from a certain website, this one, the thing is I am a complete newbie at stuff like these and aside from the hours I can't seem to get anything else.

I've been messing around with xpath code but mostly what I get is a simple []. I've been trying to get the first line of classes that correspond to the first line of hours (8.00-8.30) which should probably appear as something like this [,,,Introdução à Gestão,].

page = requests.get('https://fenix.iscte-iul.pt/publico/siteViewer.do?method=roomViewer&roomName=2E04&objectCode=4787574275047425&executionPeriodOID=4787574275047425&selectedDay=1542067200000&contentContextPath_PATH=/estudante/consultar/horario&_request_checksum_=ae083a3cc967c40242304d1f720ad730dcb426cd')
tree = html.fromstring(page.content)
class_block_one = tree.xpath('//table[@class="timetable"]/tbody/tr[1]/td[@class=*]/a/abbr//text()')
print(class_block_one)

Upvotes: 0

Views: 913

Answers (1)

Andersson
Andersson

Reputation: 52665

To get required text from first (actually second) row, you can try below XPath

'//table[@class="timetable"]//tr[2]/td/a/abbr//text()'

You can get values from all rows:

for row in tree.xpath('//table[@class="timetable"]//tr'):
    print(row.xpath('./td/a/abbr//text()'))

Upvotes: 1

Related Questions