对指定URL获取其titile
来源:互联网 发布:男生知乎 编辑:程序博客网 时间:2024/05/21 15:39
最近有个需求需要对指定一批URL获取其title,以便于筛选出所需要的URL,查了下网上工具挺多的。
我这里使用python中的urllib或urllib2包对URL 的内容提取.方法如下:
#!/usr/bin/python# -*- coding: utf-8 -*-import urllib2import reurl='http://segmentfault.com/q/1010000000124036'html = urllib2.urlopen(url).read()res_list = re.findall(r"<title>.*</title>", html)for t in res_list:print t
执行结果为:
python如何正确抓取网页标题 - SegmentFault
看这个帖子里说的BeautifulSoup也是很不错的, 没有试过,代码如下:
import urllibfrom BeautifulSoup import BeautifulSoupcontent = urllib.urlopen('http://segmentfault.com/q/1010000000124036').read()soup = BeautifulSoup(content)print soup.find('title')
用第一段代码在获取我的主页http://blog.csdn.net/lming_08?viewmode=list的title时,报错了:
Traceback (most recent call last): File "get_title.py", line 9, in <module> html = urllib2.urlopen(url).read() File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen return _opener.open(url, data, timeout) File "/usr/lib/python2.7/urllib2.py", line 400, in open response = meth(req, response) File "/usr/lib/python2.7/urllib2.py", line 513, in http_response 'http', request, response, code, msg, hdrs) File "/usr/lib/python2.7/urllib2.py", line 438, in error return self._call_chain(*args) File "/usr/lib/python2.7/urllib2.py", line 372, in _call_chain result = func(*args) File "/usr/lib/python2.7/urllib2.py", line 521, in http_error_default raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)urllib2.HTTPError: HTTP Error 403: Forbidden是403错误,看来CSDN对本次请求是禁止的, 所以我们要模拟正常人访问浏览器的行为,加上headers
url='http://blog.csdn.net/lming_08?viewmode=list'user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'# 将user_agent写入头信息headers = { 'User-Agent' : user_agent }req = urllib2.Request(url, headers = headers)html = urllib2.urlopen(req).read()res_list = re.findall(r"<title>.*\s?.*</title>", html)for t in res_list:print t[7:-8]执行没有问题,结果为:
lming_08技术博客
- 博客频道 - CSDN.NET
注意这里标题里面含有\n字符,而之前正则表达式r"<title>.*</title>"是不会包含\n的,所以改为 r"<title>.*\s?.*</title>" 或者 r"<title>.*\n?.*</title>"
最新发现这获取 http://www.smzdm.com/p/664187 title时仍然会报403错误, 查了下需要更新headers
user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'headers = { 'User-Agent' : user_agent }执行结果为:
lming_08@ubuntu:~/MyWorkSpace/Pycode/htmlparse$ python get_title.pyMentholatum 曼秀雷敦 肌研极润保湿系列套装(洁面乳50g+化妆水100ml+乳液90ml+面膜18ml+眼膜2片+眼霜3g) 99元(199-100)_乐蜂网优惠_什么值得买
在实际操作中我们会对很多url进行解析, 中间免不了会出现服务器返回其他错误代码,因此我们要捕获异常继续执行:
try: html = urllib2.urlopen(req).read() res_list = re.findall(r"<title>.*\s?.*</title>", html) for t in res_list: print t[7:-8]except urllib2.HTTPError: print "failed parsing web url"
文章参考于:
http://www.cnblogs.com/txw1958/archive/2012/03/12/2392067.html
http://segmentfault.com/q/1010000000124036
http://blog.csdn.net/my2010sam/article/details/17398807
1 0
- 对指定URL获取其titile
- 对指定URL获取其子链接
- webview 获取网页TITILE
- 指定url,获取其源代码(html)放入本页,不分本域外域
- 使用xPath技术快速获取指定节点并对其修改--(xPath技术基础)
- JavaScript获取URL指定参数
- 获取URL指定的资源
- Javasacript获取指定URL中的指定参数
- JSoup获取指定页面指定URL
- 【phpcms-v9】对{pc}标签进行二次开发,让其可以获取limit指定的文章条数
- 使用vc连接指定url获得其HTML的代码
- 使用vc连接指定url获得其HTML的代码
- 从url获取指定参数值,获取当前url上下文
- 用java 获取指定url 网站内容
- 获取指定URL网页的HTML代码
- Android获取指定URL的网页内容
- javascript获取url指定参数的值
- 获取指定url的请求内容
- Linux操作系统上安装Mysql数据库
- 第三次作业
- CodeForces - 514A Chewbaсca and Number
- 女 大三,抱金砖~呵~
- C#学习之路之使用windows media player 实例
- 对指定URL获取其titile
- CodeForces - 513A Game
- 求2个或3个正整数的最大数,用带有默认参数的函数实现
- Unity--光线投射碰撞Physics.Raycast和Physics.RaycastAll
- LeetCode - Longest Consecutive Sequence
- Leetcode: Binary Search Tree Iterator
- 【分析】dalvik虚拟机启动过程(一)
- HDU 5195 DZY Loves Topological Sorting(线段树)
- su: cannot set user id: Resource temporarily unavailable