#!/usr/bin/python3 # -*- coding: UTF-8 -*- import os import requests url='https://tv.cctv.com/2019/12/11/VIDET5sCWGWqZJQJQFLCwnB9191211.shtml' header={"Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8","Accept-Encoding":"gzip, deflate","Accept-Language":"zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2","Cache-Control":"max-age=0","Connection":"keep-alive","Upgrade-Insecure-Requests":"1","User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:77.0) Gecko/20100101 Firefox/77.0"} html=requests.get(url,headers=header) #如果网页编码为utf-8,但是获取到是乱码就加上下面的转码 html.encoding = "utf-8" title = html.text.split('')[1].split('')[0] print(title) os.system("pause")
转载请注明:零五宝典 » python 获取网页标题、内容