当前位置:   article > 正文

使用Python抓取网页信息

python抓取网络信息

        之前用C#帮朋友写了一个抓取网页信息的程序,搞得好复杂,今天朋友又要让下网页数据,好多啊,又想偷懒,可是不想用C#了,于是想到了Python,大概花了两个小时,用记事本敲的,然后在IDLE (Python GUI)里面测试。发现Python之类的解释性语言很不错,又不用编译,写个脚本就好了。代码如下:

  1. # -*- coding:gb2312 -*-
  2. import sys
  3. import urllib
  4. import re
  5. #从html中解析标题
  6. def ParshTitle(html):
  7. startPos = html.find('<title>')
  8. endpos = html.find('</title>')
  9. strTmp = html[startPos+29:endpos]
  10. strTmp = strTmp.replace('</font>', '')
  11. return strTmp
  12. #从html中解析CPI数据
  13. def ParshCPI(html):
  14. startPos = html.find('<TBODY>')
  15. endpos = html.find('</TBODY>')
  16. strTmp = html[startPos:endpos]
  17. return "<table>" + strTmp + "</table>"
  18. #从html中解析城镇投资数据
  19. def ParshUrbanInvestment(html):
  20. startPos = html.find('<TBODY>')
  21. endpos = html.find('</TBODY>')
  22. strTmp = html[startPos:endpos]
  23. return "<table>" + strTmp + "</table>"
  24. #提取各地区数据
  25. def GetHtmlData(url, htmlfile, bisCPI):
  26. wp = urllib.urlopen(url)#打开连接
  27. content = wp.read() #获取页面内容
  28. content = content.replace('\r\n', '')
  29. title = ParshTitle(content)
  30. if bisCPI:
  31. content = ParshCPI(content)
  32. else:
  33. content = ParshUrbanInvestment(content)
  34. fl = title + htmlfile
  35. #将文件路径转为gbk编码
  36. fl = unicode(fl,'gbk')
  37. f = open(fl, 'w')
  38. f.write(content)
  39. f.close()
  40. if __name__ =="__main__":
  41. #首先提取CPI数据
  42. num_list = range(72) #生成0~71的数字
  43. strUrl = "http://www.stats.gov.cn/was40/gjtjj_detail_data.jsp?searchword=%28docTitle%21%3D%B9%FA%C4%DA%C9%FA%B2%FA%D7%DC%D6%B5+and+docTitle%21%3D%B3%C7%D5%F2%B5%A5%CE%BB%B4%D3%D2%B5%C8%CB%D4%B1%C0%CD%B6%AF%B1%A8%B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%C7%F8%B3%C7%D5%F2%B5%A5%CE%BB%BE%CD%D2%B5%C8%CB%D4%B1%C0%CD%B6%AF%B1%A8%B3%EA+and++docTitle%21%3D%C8%AB%B9%FA%D6%F7%D2%AA%C5%A9%B2%FA%C6%B7%C9%FA%B2%FA%BC%DB%B8%F1%D6%B8%CA%FD+and+docTitle%21%3D%B8%F7%B5%D8%C7%F8%C5%A9%B4%E5%BE%D3%C3%F1%BC%D2%CD%A5%C6%BD%BE%F9%C3%BF%C8%CB%CF%D6%BD%F0%D6%A7%B3%F6+and++docTitle%21%3D%B8%F7%B5%D8%C7%F8%B3%C7%D5%F2%BE%D3%C3%F1%BC%D2%CD%A5%CA%D5%D6%A7%BB%F9%B1%BE%C7%E9%BF%F6%29+and+%28docTitle%3D%B8%F7%B5%D8%C7%F8%BE%D3%C3%F1%CF%FB%B7%D1%BC%DB%B8%F1%D6%B8%CA%FD+or+DOCHTMLCONTENT%3D%B8%F7%B5%D8%C7%F8%BE%D3%C3%F1%CF%FB%B7%D1%BC%DB%B8%F1%D6%B8%CA%FD%29&channelid=9951&record="
  44. for i in num_list:
  45. if i==0:
  46. continue
  47. strTemp = strUrl + str(i)
  48. strTxt = "_CPI.htm"
  49. GetHtmlData(strTemp, strTxt, True)
  50. print (str(i) + "/72")
  51. #再提取城镇投资数据
  52. num_list = range(56) #生成0~55的数字
  53. strUrl = "http://www.stats.gov.cn/was40/gjtjj_detail_data.jsp?searchword=%B8%F7%B5%D8%C7%F8%B3%C7%D5%F2%CD%B6%D7%CA&presearchword=%28docTitle%21%3D%B9%FA%C4%DA%C9%FA%B2%FA%D7%DC%D6%B5+and+docTitle%21%3D%B3%C7%D5%F2%B5%A5%CE%BB%B4%D3%D2%B5%C8%CB%D4%B1%C0%CD%B6%AF%B1%A8%B3%EA+and+docTitle%21%3D%C5%A9%C1%D6%C4%C1%D3%E6%D2%B5%D7%DC%B2%FA%D6%B5+and+docTitle%21%3D%B8%F7%B5%D8%C7%F8%C5%A9%C1%D6%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7%D2%AA%C5%A9%B2%FA%C6%B7%C9%FA%B2%FA%BC%DB%B8%F1%D6%B8%CA%FD+and+docTitle%21%3D%B8%F7%B5%D8%C7%F8%C5%A9%B4%E5%BE%D3%C3%F1%BC%D2%CD%A5%C6%BD%BE%F9%C3%BF%C8%CB%CF%D6%BD%F0%D6%A7%B3%F6+and++docTitle%21%3D%B8%F7%B5%D8%C7%F8%B3%C7%D5%F2%BE%D3%C3%F1%BC%D2%CD%A5%CA%D5%D6%A7%BB%F9%B1%BE%C7%E9%BF%F6%29+and+%28docTitle%3D%B8%F7%B5%D8%C7%F8%BE%D3%C3%F1%CF%FB%B7%D1%BC%DB%B8%F1%D6%B8%CA%FD+or+DOCHTMLCONTENT%3D%B8%F7%B5%D8%C7%F8%BE%D3%C3%F1%CF%FB%B7%D1%BC%DB%B8%F1%D6%B8%CA%FD%29&channelid=9951&record="
  54. for i in num_list:
  55. if i==0:
  56. continue
  57. strTemp = strUrl + str(i)
  58. strTxt = "_UI.htm"
  59. GetHtmlData(strTemp, strTxt, False)
  60. print (str(i) + "/56")

        对上面的代码稍微做个说明:这个主要是用来抓取国家统计局网站的统计数据,包括各地区居民消费价格指数和各地区城镇投资数据。同学要的数据是2006年1月到现在的,在统计局的网站上查询发现,所有的数据的URL都一样,只有最后的一个id值是从1开始递增,最近的是1,时间之前的慢慢递增。统计局的这个URL好长啊!!

        代码中写了三个函数,分别是获取网页的Tilte,用来作为保存的文件的文件名。

        一个函数是从html中解析各地区居民消费价格指数,另外一个提取各地区城镇投资数据,其实目前的代码这两个函数是一样的,我比较懒,没有提取里面的详细信息,目前只是把网页的那个Table的tbody给拿出来了,呵呵。如果可以的话,可以使用正则表达式,只把里面的数据提取出来。

        上次用C#写了好久才搞定,Python果然不一样,写简单的工具的话,还是比较推荐Python的,第一,简单,第二,不用编译,直接写个txt,放到解释器里面就好了。


转载于:https://www.cnblogs.com/xiaowangba/archive/2012/02/27/6314030.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/1000877
推荐阅读
相关标签
  

闽ICP备14008679号