我试图在Replit lxml请求,我不明白为什么它不起作用。该程序不会停止运行,直到最大重试,在那里我得到这个错误:lxml请求repl.it
Traceback (most recent call last): File "python", line 6, in requests.exceptions.ConnectionError: HTTPConnectionPool(host='www.presidency.ucsb.edu', port=80): Max retries exceeded with url: /ws/index.php?pid=29400.html (Caused by NewConnectionError(': Failed to establish a new connection: [Errno -2] Name or service not known',))
我的代码非常简单:
from lxml import html
import requests
url = 'http://www.presidency.ucsb.edu/ws/index.php?pid=29400.html'
r = requests.get(url)
tree = html.fromstring(r.content)
text = tree.xpath('//span[@class="displaytext"]/text()')
print(text)
我怎样才能得到它来运行?我试图获取位于“displaytext”span class中的该网站的内容。我一直在使用this Python guide作为参考。
Python版本3.5
当我运行你的代码时,我确实得到了输出:>>> print text [“HOWARD K. SMITH,主持人:晚上好,美国的电视台和广播电台以及他们的附属电台都很自豪为两位主要候选人在当前政治运动中为讨论问题提供了便利,候选人无需介绍,共和党候选人......等等......''你是否通过pip安装了请求模块? python版本?(我的是2.7) –
ps:我直接在python中工作。repl.it不允许我导入请求 –
所以问题是在repl.it中提取请求谢谢:) –