(1)

import requests
from lxml import etree

url = "http://renwen.sanyau.edu.cn/?article/14263.html"

(2)

res = requests.get(url)

(3)

print(res.status_code)

输出结果为:200

(4)

print(res.encoding)

输出结果为:utf-8

(5)

html = etree.HTML(res.text)
newtitle = html.xpath("//div[@class='newsTitle']/h1/text()")[0]
print(newtitle)

输出结果为:人文与传播学院学子在2023年(16届)中国大学生计算机设计大赛省赛中荣获佳绩

1导入相关的库将网址httprenwensanyaueducnarticle14263html赋值给变量url2结合网站链接生成get请求赋值给变量res3查看服务器的状态码用print语句输出4查看服务器返回的编码用print语句输出5使用xpath解析网页输出这页新闻标题人文与传播学院学子在2023年16届中国大学生计算机设计大赛省赛中荣获佳绩标题名称存在变量newtitle中用print语句

原文地址: https://www.cveoy.top/t/topic/gAtg 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录