【原创】 linux-python …
来源:互联网 发布:淘宝c店被投诉产品三无 编辑:程序博客网 时间:2024/05/21 02:22
linux 下pythonspider获取网页title
原创所有,转载请以超链接附明本文地址及作者,谢谢http://blog.sina.com.cn/s/blog_83dc494d0101c20q.html
------------------------------------
#!/usr/bin/python
#!coding:utf-8
import re
import urllib
urls=['http://www.baidu.com','http://www.hao123.com']
i = 0
regex=''
#pattren = re.compile(regex)
while i < len(urls):
---------------------------------------------------
说明,使用pythonurllib模块和re(正则)模块。获取网页html文件,使用正则匹配title标签。代码如上文以及下图所展示:
当然,你可以import sys,使用sys.argv 来获取你想要的网页title。
下面展示代码,有兴趣自己百度或者谷歌如何进行的,很简单的东西,太小儿科了,但是考虑可能有的同学真的需要,就写一下吧。(爬虫获取到url,title,类别。。。)
--------------------------------------
#!/usr/bin/python
#!coding:utf-8
import re
import urllib
import sys
def get_title(urls):
if __name__== '__main__':
-----------------------------
说明:因为代码比较简短,不加注释了。
使用:
zhangzhipeng@zhangzhipeng-K53SD:~/py/spider_test$ pythonget-title-argv.py http://www.baidu.com http://www.google.comhttp://www.hao123.com
http://www.baidu.com 百度一下,你就知道
http://www.google.com Google
http://www.hao123.com hao123_上网从这里开始
zhangzhipeng@zhangzhipeng-K53SD:~/py/spider_test$
这里有一个问题的,如果网页的编码格式为GB2312的会乱码。
所以最好判断一下编码
china.com ---> charset="GB2312">
qq.com --->http-equiv="Content-Type"content="text/html;charset=gb2312">
zhangzhipeng2023.cn ---> property="qc:admins" content="156636705167201056720236367 "/>
咳咳...我的这个渣网站都没写charset...
抓到以后我记得GB2312的转成iso-8859-01貌似可以正常显示,试试看- .-
----------------------
#!/usr/bin/python
#!coding:utf-8
import re
importurllib
import sys
defget_title(urls):
if len(charset[0]) ==1:
charset =charset[0][0]
elif len(charset[0]) ==2:
charset =charset[0][1]
else:
-----------------------
我只能做到这里了
python处理编码真是硬伤...中华网明明是gb2312还是解码错误...
如果有对pythonencoding擅长的,希望回帖求指点。
原创所有,转载请以超链接附明本文地址及作者,谢谢http://blog.sina.com.cn/s/blog_83dc494d0101c20q.html
0 0
- 【原创】 linux-python …
- 【原创】 linux-python …
- 【原创】 linux-python …
- 【原创】 linux-python …
- 【原创】 linux-python …
- 【原创】 linux-python …
- 【原创】 linux-python …
- 【原创】 linux-python …
- 【原创】 linux Python&…
- 【原创】 linux Python 列表整合
- 【原创】linux-python收藏篇 …
- 【原创】 linux-python-redis…
- 【原创】 linux-python-MySQL…
- 【原创】python try cat…
- 【原创】python The pac…
- 【原创】 python django…
- 【原创】 python django…
- 【原创】 python django…
- 【原创】 python simplegui
- 【原创】 linux-ubuntu …
- C、c++输入输出符号
- 【原创】Linux-Ubuntu 12.04&…
- 287@365 自大
- 【原创】 linux-python …
- 【原创】linux 查看命令的安装位置
- 【原创】 linux-python …
- 【原创】 linux-python …
- 【原创】 linux-python …
- C#中应用定制特性来进行运行期判断
- 【原创】mysql-python安装 dj…
- 【原创】 linux-python …
- 【原创】linux-python收藏篇 …