python使用get在百度搜索并保存第一页搜索结果
作者:vpoet
mail:vpoet_sir@163.com
注:随意copy,不用在意我的感受
1 #coding:utf-8 2 import urllib 3 import urllib2 4 import re 5 6 """ 7 使用GET在百度搜索引擎上查询 8 此例演示如何生成GET串,并进行请求. 9 """ 10 if __name__=="__main__": 11 url = "http://www.baidu.com/s" 12 search = [('w','QQ')] 13 getString = url + "?" + urllib.urlencode(search) 14 15 req = urllib2.Request(getString) 16 fd = urllib2.urlopen(req) 17 baiduResponse="" 18 while 1: 19 data= fd.read(1024) 20 if not len(data): 21 break 22 baiduResponse+=data 23 fobj=open("baidu.html",'w') 24 fobj.write(baiduResponse) 25 fobj.close()
搜索QQ保存的网页截图为:
打开看看:
再和百度搜索QQ的结果对比:
对的,是一样的。我们保存的网页没有图片是因为我们只保存的网页的html结构并没有将其中的图片链接从服务器上下载下来,懂了吧