Python get Baidu Get search results #-*-coding:cp936-*-import Urllib2,urllib,sys,io "" "Use get query on Baidu search engine This example shows how to generate a get string and make a request." "" url = "http://www.baidu.com/s" search = [(' W ', ' codemo ')]getstring = URL + "?" + urllib.urlencode (search) req = urllib2. Request (getString) fd = Urllib2.urlopen (req) baiduresponse= "" While 1: data= fd.read (1024x768) if not len (data): Break Baiduresponse+=datafobj=open ("baidu.html", ' W ') Fobj.write (baiduresponse) fobj.close ()
Python Baidu login Import sys, urllib2,gzip,stringioparams = "charset=utf-8&codestring=&token= 96f08093303c5c0b3f4a62acb8c04898&isphone=false&index=0&u=http%3a%2f%2fwww.baidu.com%2f&safeflg =0&staticpage=https%3a%2f%2fpassport.baidu.com%2fv2jump.html&logintype=1&tpl=mn&callback= Parent.bdpass.api.login._postcallback&username=codemo&password=codemopass&verifycode=&mem_pass =on "headers = {" Accept ":" Image/gif, */* "," Referer ":" Https://passport.baidu.com/v2/?login&tpl=mn&u=http %3a%2f%2fwww.baidu.com%2f "," Accept-language ":" ZH-CN "," Content-type ":" application/x-www-form-urlencoded "," Acc Ept-encoding ":" gzip, deflate "," user-agent ":" Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1;. NET CLR 2.0.50727) "," Host ":" Passport.baidu.com "," Connection ":" Keep-alive "," Cache-control ":" No-cache " }request = Urllib2. Request (url = ' Https://passport.baidu.com/v2/api/?login ', data = params,headers=headers) responsE = Urllib2.urlopen (Request) if Response.info (). Get (' content-encoding ') = = ' gzip ': print ' gzip enabled ' BUF = Stringi O.stringio (Response.read ()) F = gzip. Gzipfile (fileobj=buf) data = F.read () Else:data = Response.read () print "Success-----------------", "\ n", data
Python Post, get Baidu