我需要为我的项目创建instagram登录表单。我已经编写了这段代码,但它无法正常工作。我需要获得' sessionid'请求后的cookie
@IBDesignable
class Label: UILabel {
override var intrinsicContentSize: CGSize {
var size = super.intrinsicContentSize
let padding = (1.0 - lineHeightMultiple) * font.pointSize
size.height += padding
return size
}
override var text: String? {
didSet {
updateAttributedText()
}
}
@IBInspectable var lineHeightMultiple: CGFloat = 1.0 {
didSet {
updateAttributedText()
}
}
private func updateAttributedText() {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineHeightMultiple = lineHeightMultiple
attributedText = NSAttributedString(string: text ?? "", attributes: [
.font: font,
.paragraphStyle: paragraphStyle,
.foregroundColor: textColor
])
invalidateIntrinsicContentSize()
}
}
以下是回复标题:
def authorize_inst():
url = 'https://www.instagram.com/'
url_main = url + 'accounts/login/ajax/'
req1 = requests.get(url)
print(req1.headers)
print(req1.cookies['csrftoken'])
print('-----')
auth = {'username':'login','password':'pass'}
req2 = requests.post(url_main,cookies={'csrftoken':req1.cookies['csrftoken']},data=auth,allow_redirects=True)
print(req2.headers)
print(req2.cookies)
来自`{'Content-Type': 'text/html', 'X-Frame-Options': 'SAMEORIGIN', 'Cache-Control': 'private, no-cache, no-store, must-revalidate', 'Pragma': 'no-cache', 'Expires': 'Sat, 01 Jan 2000 00:00:00 GMT', 'Vary': 'Cookie, Accept-Language', 'Content-Language': 'en', 'Access-Control-Allow-Origin': 'https://www.instagram.com/', 'Date': 'Sat, 17 Feb 2018 08:46:12 GMT', 'Strict-Transport-Security': 'max-age=86400', 'Set-Cookie': 'rur=FTW; Path=/, csrftoken=KSGEZBQrpQBQ8srEcK98teilzOsndDcF; expires=Sat, 16-Feb-2019 08:46:12 GMT; Max-Age=31449600; Path=/; Secure, mid=Wofr1AAEAAGPK-9pKoyWokm4jRo8; expires=Fri, 12-Feb-2038 08:46:12 GMT; Max-Age=630720000; Path=/', 'Connection': 'keep-alive', 'Content-Length': '21191'`}
的部分代码:
req2.content
有什么问题?提前谢谢。
答案 0 :(得分:1)
如果您使用requests.Session()
,则不需要cookie
标题。
对您的解决方案进行一些更改;你可以用这个:
import requests
url = 'https://www.instagram.com/accounts/login/'
url_main = url + 'ajax/'
auth = {'username': 'login', 'password': 'pass'}
headers = {'referer': "https://www.instagram.com/accounts/login/"}
with requests.Session() as s:
req = s.get(url)
headers['x-csrftoken'] = req.cookies['csrftoken']
s.post(url_main, data=auth, headers=headers)
# Now, you can use 's' object to 'get' response from any instagram url
# as a logged in user.
r = s.get('https://www.instagram.com/accounts/edit/')
# If you want to check whether you're getting the correct response,
# you can use this:
print(auth['username'] in r.text) # which returns 'True'
答案 1 :(得分:0)
我找到了决定。这是代码:
import requests
url = 'https://www.instagram.com/accounts/login/'
url_main = url + 'ajax/'
req1 = requests.get(url)
auth = {'username': 'login', 'password': 'pass'}
headers = {
'origin': "https://www.instagram.com",
'x-instagram-ajax': "1",
'content-type': "application/x-www-form-urlencoded",
'accept': "*/*",
'x-requested-with': "XMLHttpRequest",
'user-agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.167 Safari/537.36",
'x-csrftoken': req1.cookies['csrftoken'],
'referer': "https://www.instagram.com/accounts/login/",
'accept-encoding': "gzip, deflate, br",
'accept-language': "ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7",
'cookie': "fbm_124024574287414=base_domain=.instagram.com; ig_or=landscape-primary; ig_pr=1; ig_vw=1920; mid=Wcp3vAALAAGjSka8GEnPwijnia6-; rur=FTW; ig_vh=949; csrftoken="+req1.cookies['csrftoken']+"; fbsr_124024574287414=jSVsTpoGuOgZQB0vEP_X70hrO2-LlfD9EnUz9nwGTXo.eyJhbGdvcml0aG0iOiJITUFDLVNIQTI1NiIsImNvZGUiOiJBUUMyM1FOT2ZwQU1oRVVudldzeGp1dHpwckEyODBLbUZseVo4VjVMMVVRVkJYbUVadHFyd25nekdtdzg2ejFTajRIYzVSWVRISHlvTjZXU29ScEdDZXB5RnRTMDloRXlLT3dXbU5uTTloQV9PTFE2VUI2ZFhPWW5Qa3pBNlNSZkFpSWZiU2N2anEyRFZna2FMdkdDWkRBQklCbElhYVAya2JNZzJBQW9fU0lzS3Z5NDhHRXB2RjFwQmdKOHNrdjltZWtYbFF1Z1dib040UXlzM2lwUTVfRUsxTjJUaHBpb3g1QUF2SDNpSVE2Qm1fdTFSeTZTVHFZMWR1M2NCSU5FRHpiZXRaRjFvSXY1MGJzLWFWQk4tcmFsVHY1dGE2VS13ajZCUXE0UlFEQjVHZEdqeDZpZkdlc0JsYnZvQUNlVFFJQ3pVSl9id1F1eGpyc0UxbEFzalRWZCIsImlzc3VlZF9hdCI6MTUxODg4NDA1MCwidXNlcl9pZCI6IjEwMDAyMzcyMDI5NTQyNyJ9",
'x-compress': "null",
'cache-control': "no-cache"
}
req2 = requests.post(url_main,data=auth,headers=headers)
sessionid = req2.cookies['sessionid']
print(req2.content)
print(req2.cookies['ds_user_id'])
print(req2.cookies['sessionid'])