Python抓取网页

[python]  view plain copy
  1. #!/usr/bin/env python  
  2. # 1.py  
  3. # use UTF-8  
  4. # Python 3.3.0  
  5.   
  6. # get code of given URL as html text string  
  7. # Python3 uses urllib.request.urlopen()  
  8. # instead of Python2's urllib.urlopen() or urllib2.urlopen()  
  9. # http://blog.csdn.net/zsuguangh/article/details/6226385  
  10. import urllib.request  
  11.   
  12. fp = urllib.request.urlopen("http://www.baidu.com")  
  13. mybytes = fp.read()  
  14.   
  15. # note that Python3 does not read the html code as string  
  16. # but as html code bytearray, convert to string with  
  17. mystr = mybytes.decode("utf8")      # 说明接收的数据是UTF-8格式(这样子可以解析和显示中文)  
  18.   
  19. fp.close()  
  20.   
  21. print(mystr)  


---------------------------------------------------------------------------------------------------------------------------------------------------------

2. 分析html的编码方式(其实就是字符串的分析)

---------------------------------------------------------------------------------------------------------------------------------------------------------

[python]  view plain copy
  1. #!/usr/bin/env python  
  2. # 2.py  
  3. # use UTF-8  
  4. # Python 3.3.0  
  5.   
  6. # get the code of a given URL as html text string  
  7. # Python3 uses urllib.request.urlopen()  
  8. # get the encoding used first  
  9. # tested with Python 3.1 with the Editra IDE  
  10.   
  11. import urllib.request  
  12.   
  13. def extract(text, sub1, sub2):  
  14.     """ 
  15.     extract a substring from text between first 
  16.     occurances of substrings sub1 and sub2 
  17.     """   
  18.     return text.split(sub1, 1)[-1].split(sub2, 1)[0]  
  19.   
  20. fp = urllib.request.urlopen("http://www.baidu.com")                     # 打开URL  
  21. mybytes = fp.read()                         # 读取HTML信息  
  22.   
  23. encoding = extract(str(mybytes).lower(), 'charset=''"')       # 查找HTML数据中"charset"字符, 找到编码方式  
  24. print('-'*50)  
  25. print"Encoding type = %s" % encoding )  
  26. print('-'*50)  
  27.   
  28. if encoding:  
  29.     # note that Python3 does not read the html code as string  
  30.     # but as html code bytearray, convert to string with  
  31.     mystr = mybytes.decode(encoding)  
  32.     print(mystr)  
  33. else:  
  34.     print("Encoding type not found!")  
  35.   
  36. fp.close()  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值