【问题标题】:How to scrape a web site using Python, Requests and Xpath?如何使用 Python、Requests 和 Xpath 抓取网站?
【发布时间】:2019-04-26 20:20:20
【问题描述】:

我尝试使用下面的代码在此网页 (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)

【问题讨论】:

标签: python web-scraping python-requests lxml


【解决方案1】:

您可以尝试使用这个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)

【讨论】:

  • 只支持python 3.6...我会回到selenium...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多