我使用requests.get()
申请了一个网站。 Cookie现在位于r.cookies
。
比我发送我的请求到服务器,但服务器扔我400错误。
当我在调试日志中仔细观察时,我看到它发送了带有双反斜杠的cookie(=r.cookies
)。但是当我使用print(r.cookies)
或print(r.cookies[cookieName])
打印它时,它只打印一个反斜杠
我知道我需要单反斜杠,因为谷歌Chrome调试工具说,页面发送它。
Google Chrome会发送单反斜杠。
源代码:
import requests
import time
import json
import math
import random
import logging
try:
import http.client as http_client
except ImportError:
# Python 2
import httplib as http_client
http_client.HTTPConnection.debuglevel = 1
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True
def _base36encode(integer):
chars, encoded = '0123456789abcdefghijklmnopqrstuvwxyz', ''
while integer > 0:
integer, remainder = divmod(integer, 36)
encoded = chars[remainder] + encoded
return encoded
def _register(firstName, lastName, email, password, captchaApiKey):
startTime = int(1e3 * time.time())
url = 'https://www.quora.com'
httpsProxy = 'https://207.99.118.74:8080'
proxy = {"https" : httpsProxy}
headers = {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'Accept-Language':'cs-CZ,cs;q=0.8,en;q=0.6','Cache-Control':'max-age=0','Connection':'keep-alive','Host':'www.quora.com','Upgrade-Insecure-Requests':'1','User-Agent':'Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'}
r = requests.get(url, headers=headers, proxies=proxy)
quoraHtml = r.text
data = {}
data['proxy'] = proxy
data['cookies'] = r.cookies
print (data['cookies'])
######################
## get certification tokens ##
#####################
pos1 = quoraHtml.find('"formkey": "')
pos2 = quoraHtml.find('"', pos1+13)
data['formKey'] = quoraHtml[pos1+12:pos2] #form key for certification
pos1 = quoraHtml.find('"postkey": "', pos2)
pos2 = quoraHtml.find('"', pos1+13)
data['postKey'] = quoraHtml[pos1+12:pos2] #form key for certification
pos1 = quoraHtml.find('"serverCallUrl": "/')
pos2 = quoraHtml.find('"', pos1+19)
data['sendUrl'] = 'https://www.quora.com' + quoraHtml[pos1+18:pos2] #url to send data
pos1 = quoraHtml.find('"windowId": "', pos2)
pos2 = quoraHtml.find('"', pos1+14)
data['windowId'] = quoraHtml[pos1+13:pos2] #form key for certification
pos1 = quoraHtml.find('"SignupValidator", "', pos2)
pos2 = quoraHtml.find('"', pos1+21)
firstNameValidateEncrypted = quoraHtml[pos1+20:pos2] #encrypted first name validator
pos1 = quoraHtml.find('"SignupValidator", "', pos2)
pos2 = quoraHtml.find('"', pos1+21)
lastNameValidateEncrypted = quoraHtml[pos1+20:pos2] #encrypted last name validator
pos1 = quoraHtml.find('"SignupValidator", "', pos2)
pos2 = quoraHtml.find('"', pos1+21)
mailValidateEncrypted = quoraHtml[pos1+20:pos2] #encrypted email validator
pos1 = quoraHtml.find('"SignupValidator", "', pos2)
pos2 = quoraHtml.find('"', pos1+21)
passValidateEncrypted = quoraHtml[pos1+20:pos2] #encrypted password validator
pos1 = quoraHtml.find('"hmacs": {')
pos2 = quoraHtml.find('}', pos1+11)
formDecryptKeyListText = quoraHtml[pos1+9:pos2+1] #decryption key list in string format
formDecryptKeyList = json.loads(formDecryptKeyListText) #decryption key list
data['firstNameValidate'] = formDecryptKeyList[firstNameValidateEncrypted]
data['lastNameValidate'] = formDecryptKeyList[lastNameValidateEncrypted]
data['mailValidate'] = formDecryptKeyList[mailValidateEncrypted]
data['passValidate'] = formDecryptKeyList[passValidateEncrypted]
data['e2e'] = _base36encode(int(1e3 * startTime + math.floor(1e3 * random.random())))
##############
## validate data ##
##############
_validate('first_name',firstName,data)
def _validate(validate,value,formData):
data = {}
data['json'] = '{"args":[],"kwargs":{"value":"'+value+'"}}'
data['formKey'] = formData['formKey']
data['postKey'] = formData['postKey']
data['window_id'] = formData['windowId']
data['referring_controller'] = 'index'
data['referring_action'] = 'index'
if validate == 'first_name':
data['__vcon_json'] = '["'+formData['firstNameValidate']+'"]'
elif validate == 'last_name':
data['__vcon_json'] = '["'+formData['lastNameValidator']+'"]'
elif validate == 'email':
data['__vcon_json'] = '["'+formData['mailValidator']+'"]'
elif validate == 'password':
data['__vcon_json'] = '["'+formData['passValidator']+'"]'
data['__vcon_method'] = 'validate'
data['__e2e_action_id'] = formData['e2e']
data['js_init'] = '{"id":"@'+validate+'"}'
data['__metadata'] = '{}'
headers = {'Accept': 'application/json, text/javascript, */*; q=0.01', 'Accept-Language':'cs-CZ,cs;q=0.8,en;q=0.6','Connection':'keep-alive','Content-Type':'application/x-www-form-urlencoded; charset=UTF-8','Host':'www.quora.com','Origin':'https://www.quora.com','Referer':'https://www.quora.com/','User-Agent':'Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36','X-Requested-With':'XMLHttpRequest'}
r = requests.post(formData['sendUrl'], data=data, headers=headers, cookies=formData['cookies'], proxies=formData['proxy'])
print (r.status_code)
def createAccount(captchaApiKey):
_register('First','Last','mail@gmail.co','abcd123*',captchaApiKey)
captchaApiKey = 'b6d955900d620d2589c03fef5239a1be'
createAccount(captchaApiKey)