Python lxml xpath 无输出

Python lxml xpath no output

出于教育目的,我正在尝试使用 lxml 和 Python 中的请求来抓取 this page

具体来说我只想在页面上打印所有教授的研究领域。 这是我到目前为止所做的

import requests
from lxml import html

response=requests.get('http://cse.iitkgp.ac.in/index.php?secret=d2RkOUgybWlNZzJwQXdLc28wNzh6UT09')
parsed_body=html.fromstring(response.content)

for row in parsed_body.xpath('//div[@id="maincontent"]//tr[position() mod 2 = 1]'):
    for column in row.xpath('//td[@class="fcardcls"]/tr[2]/td/font/text()'):        
        print column.strip()    

但是它没有打印任何东西。我在 xpaths 上苦苦挣扎,最初使用 chrome 中的复制 xpath 功能。我遵循了以下 SO questions/answers 中所做的,并相当多地清理了我的代码并摆脱了 xpaths 中的“ tbody ”。还是代码returns一片空白。

2. Python-lxml-xpath problem

首先,包含所需数据的主要内容是通过 XHR 请求从不同端点加载的 - 在您的代码中进行模拟。

这是完整的工作代码打印名称和每个名称的研究领域列表:

import requests
from lxml import html

response = requests.get('http://cse.iitkgp.ac.in/faculty4.php?_=1450503917634')
parsed_body = html.fromstring(response.content)

for row in parsed_body.xpath('.//td[@class="fcardcls"]'):
    name = row.findtext(".//a[@href]/b")
    name = ' '.join(name.split())  # getting rid of multiple spaces

    research_areas = row.xpath('.//*[. = "Research Areas: "]/following-sibling::text()')[0].split(", ")

    print(name, research_areas)

这里的想法是利用所有 "professor blocks" 都位于具有 class="fcardcls"td 个元素中这一事实。对于每个块,从 Research Areas: 粗体文本后的以下字符串中的粗体 link 文本和研究领域中获取名称。