Created
February 20, 2014 03:15
-
-
Save Kevinstronger/9106489 to your computer and use it in GitHub Desktop.
get Baidu Translation without API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import urllib2 | |
import urllib | |
import json | |
import httplib | |
def savefile(filename,data): | |
fdata = open(filename,'wb') | |
fdata.write(data) | |
fdata.close | |
def checkPostdata(): | |
values ={'query':'some thing'} | |
url_values = urllib.urlencode(values) | |
url = 'http://fanyi.baidu.com/langdetect' | |
req = urllib2.Request(url, url_values) | |
resdata=urllib2.urlopen(req).read() | |
return resdata | |
def checkGetdata(): | |
data = {} | |
data['query'] = 'усталость и сонливость в взрослы' #"some thing",'De la fatigue et de la somnolence chez','中关村' | |
url_values = urllib.urlencode(data) | |
url = 'http://fanyi.baidu.com/langdetect' | |
full_url = url + '?' + url_values | |
print full_url | |
req=urllib2.Request(full_url) | |
resdata=urllib2.urlopen(req).read() | |
return resdata | |
def checkFakeBrowser(): | |
data = {} | |
data['from']='en' | |
data['to']='zh' | |
data['query']='hello world' | |
url_values = urllib.urlencode(data) | |
#url = 'http://fanyi.baidu.com/langdetect' | |
url = 'http://fanyi.baidu.com/v2transapi' | |
req = urllib2.Request(url, url_values) | |
req.add_header( "Cookie" , 'AIDUID=DDA762F02FD0392E14C483EB7C95E0DE:FG=1; BDREFER=%7Burl%3A%22http%3A//tech.baidu.com/%22%2Cword%3A%22%22%7D; cflag=65535%3A1; BDUSS=nk4VVVQTHNUTXhyNFUtT3JENkQteXVMRVRMfnBvT0k5NGg0UEx6cVU1alVrTFpTQVFBQUFBJCQAAAAAAAAAAAEAAAC3Xw8LZ29uZ3N0YXIyMDAwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAANQDj1LUA49SM; H_PS_PSSID=3630_1445_4266_4261_3594; locale=zh; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1385105260; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1385105797') | |
req.add_header( "Referer", "http://fanyi.baidu.com/" ) | |
req.add_header("User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36") | |
resdata=urllib2.urlopen(req).read() | |
return resdata | |
#加上session | |
def checkHttpSession(): | |
import cookielib | |
import socket | |
##设置超时 | |
socket.setdefaulttimeout(5) | |
cj = cookielib.CookieJar() | |
cjhandler=urllib2.HTTPCookieProcessor(cj) | |
opener = urllib2.build_opener(cjhandler) | |
urllib2.install_opener(opener) | |
opener.handle_open["http"][0].set_http_debuglevel(1) | |
##加上Basic认证 | |
# password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm() | |
# top_level_url = "http://www.163.com/" | |
# password_mgr.add_password(None, top_level_url, username, password) | |
# handler = urllib2.HTTPBasicAuthHandler(password_mgr) | |
# opener = urllib2.build_opener(handler) | |
# urllib2.install_opener(opener) | |
##使用代理 | |
# proxy_support = urllib2.ProxyHandler({"http":"http://1.2.3.4:3128/"}) | |
# opener = urllib2.build_opener(proxy_support) | |
# urllib2.install_opener(opener) | |
def checkhttpConnetion(): | |
#httplib.HTTPConnection.debuglevel = 1 | |
conn = httplib.HTTPConnection("www.sina.com.cn", 80, False) | |
conn.request('get', '/', headers = {"Host": "www.163.com", | |
"User-Agent": "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1) Gecko/20090624 Firefox/3.5", | |
"Accept": "text/plain"}) | |
res = conn.getresponse() | |
print 'version:', res.version | |
print 'reason:', res.reason | |
print 'status:', res.status | |
print 'msg:', res.msg | |
print 'headers:', res.getheaders() | |
#html | |
#print '\n' + '-' * 50 + '\n' | |
#print res.read() | |
data = res.read() | |
conn.close() | |
return data | |
#使用Basic HTTP Authentication | |
def checkBaseAuth(): | |
# Create an OpenerDirector with support for Basic HTTP Authentication... | |
auth_handler = urllib2.HTTPBasicAuthHandler() | |
auth_handler.add_password(realm='PDQ Application', | |
uri='https://pythoneye.com/vecrty.py', | |
user='user', | |
passwd='pass') | |
opener = urllib2.build_opener(auth_handler) | |
# ...and install it globally so it can be used with urlopen. | |
urllib2.install_opener(opener) | |
data = urllib2.urlopen('http://www. pythoneye.com/app.html') | |
return data | |
#使用代理ProxyHandler | |
def checkProxy(): | |
from urllib2 import Request, urlopen, URLError, HTTPError | |
proxy_handler = urllib2.ProxyHandler({'http': 'http://www.example.com:3128/'}) | |
proxy_auth_handler = urllib2.HTTPBasicAuthHandler() | |
proxy_auth_handler.add_password('realm', 'host', 'username', 'password') | |
opener = build_opener(proxy_handler, proxy_auth_handler) | |
# This time, rather than install the OpenerDirector, we use it directly: | |
opener.open('http://www.example.com/login.html') | |
if __name__ == '__main__': | |
print "test here" | |
checkHttpSession() | |
#data = checkGetdata() | |
data = checkPostdata() | |
#data = checkFakeBrowser() | |
print data.encode('utf-8') | |
savefile('s.html',data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment