你是一个资深程序员现在需要满足五条要求编写代码并成功运行避免出现NameError name requsts is not defined的问题要求为:1导入相关的库将网址httprenwensanyaueducnarticle14263html赋值给变量url2结合网站链接生成get请求赋值给变量res3查看服务器的状态码用print语句输出4查看服务器返回的编码用print语句输出5使用xpa
import requests from lxml import html
url = "http://renwen.sanyau.edu.cn/?article/14263.html" res = requests.get(url)
print("服务器状态码:", res.status_code) print("服务器编码:", res.encoding)
tree = html.fromstring(res.text) newtitle = tree.xpath('//div[@class="article-title"]/h2/text()')[0] print(newtitle)
原文地址: https://www.cveoy.top/t/topic/gAwt 著作权归作者所有。请勿转载和采集!