如何使用 Python、Requests 和 Xpath 抓取网站?

How to scrape a web site using Python, Requests and Xpath?

我尝试使用下面的代码在此网页 (https://www.meleenumerique.com/scientist_comite) 上抓取人员的名字 + 姓氏,但它不起作用。我怎样才能确定它出了什么问题?

这是我写的代码

from lxml import html  
import csv,os,json
import requests
url="https://www.meleenumerique.com/scientist_comite"
r=requests.get(url)
t=html.fromstring(r.content)

title=t.xpath('/html/head/title/text()')
#Create the list of speaker
speaker=t.xpath('//span[contains(@class,"speaker-name")]//text()')

print(title)
print("Speakers:",speaker)

您可以尝试使用这个 Requests-HTML 库,它应该可以让您从该页面抓取内容。此库支持 xpath 并具有处理动态内容的能力。

import requests_html

session = requests_html.HTMLSession()
r = session.get('https://www.meleenumerique.com/scientist_comite')
r.html.render(sleep=5, timeout=8)
for item in r.html.xpath("//*[contains(@class,'speaker-name')]"):
    print(item.text)