加载中…
个人资料
蜀山剑侠endurer
蜀山剑侠endurer
  • 博客等级:
  • 博客积分:0
  • 博客访问:7,238
  • 关注人气:22
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

python获取网页标题title的几种方法代码

(2021-04-16 11:59:43)
标签:

python

网页

标题

title

分类: 开源世界
# -*- coding:UTF-8 -*-
import urllib.request
import re

#url = 'http://bing.com'
url ='http://www.mengxi.net/book/263745/index.html'
page = urllib.request.urlopen(url,data = None)
html = page.read().decode('utf-8')
# or html=urllib.urlopen(url).read()

#print (html)

# Python3 findall数据类型用bytes类型


#方法1:无法读取包含\r\n\t的标题文本
title = re.findall(r'',html)
print (title)

#方法2:无法过滤换行符
con = re.findall(r'', html, re.S|re.M)
print (con)

#方法3
tag = 'title'
tag_pat = r'(?<=<'+ tag + '>).*?(?=</' + tag + '>)'  
tag_ex = re.compile(tag_pat, re.M|re.S)  
tag_obj = re.search(tag_ex, html)
con = tag_obj.group()
print (con)

#运行情况如下:
#D:\Python34\works>py title.py
#[]
#['\r\n\t续蜀山剑侠传最新章节首家独发 - 风叶如笛 - 武侠仙侠 - 梦溪文学网\r\n']
#
#        续蜀山剑侠传最新章节首家独发 - 风叶如笛 - 武侠仙侠 - 梦溪文学网

python获取网页标题title的几种方法代码

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有