问题描述
我正在尝试使用Python进入我的Amazon帐户的订单页面,但是它一直在报告我必须启用cookie。但是我正在使用一个会话对象,该对象应该保留从成功登录响应到获取订单页面请求的cookie。但是我从来没有在收到请求时检索订单页面,因为亚马逊认为未启用Cookie。但是cookie应该已经在会话中传递了。
有一次,我确实专门读取了cookie,并将它们作为session.header的一部分传递回去,它仍然报告未启用cookie。有什么想法为什么亚马逊在请求获取订单页面的请求中传递cookie时不启用cookie?在成功检索订单页面之前,我无法对其进行处理。
'''load packages'''
import os
import requests
from bs4 import BeautifulSoup
''' -------- Get Amazon login page --------'''
'''define URL where login form is located'''
site = 'https://www.amazon.com/gp/sign-in.html'
'''initiate session'''
session = requests.Session()
'''define session headers'''
session.headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/44.0.2403.61 Safari/537.36','Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8','Accept-Language': 'en-US,en;q=0.5','Referer': site
}
'''get login page'''
resp = session.get(site)
html = resp.text
'''get BeautifulSoup object of the html of the login page'''
soup = BeautifulSoup(html,'lxml')
'''scrape login page to get all the needed inputs required for login'''
data = {}
form = soup.find('form',{'name': 'signIn'})
for field in form.find_all('input'):
try:
data[field['name']] = field['value']
except:
pass
'''add username and password to the data for post request'''
data[u'email'] = 'name@domain.com' ''' users amazon username '''
data[u'password'] = 'password' ''' amazon account password '''
''' Post the sign in form to Amazon'''
post_resp = session.post('https://www.amazon.com/ap/signin',data = data)
''' -------- See if we are signed in --------'''
''' Determine if we are logged in'''
post_soup = BeautifulSoup(post_resp.content,'lxml')
if post_soup.find_all('title')[0].text == 'Amazon Sign-In':
print('****Login Successfull')
else:
print('****Login Failed')
os.system("pause")
exit()
''' -------- Retreive my orders --------'''
site = 'https://www.amazon.com/gp/css/order-history?ref_=nav_orders_first'
post = session.get(site)
html = resp.text
'''get BeautifulSoup object of the html of the login page'''
soup = BeautifulSoup(html,'lxml')
if soup.text.find("Enable cookies"):
print ("\nCookies are not enabled - request failed.")
else:
print ("\nData has been successfully retrieved.")
session.close()
os.system("pause")
exit()
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)