我正在尝试使用机械化将图像上传到ebay-kleinanzeigen.de,但是每次尝试时,都会出现403错误。
登录代码:
import mechanicalsoup
browser = mechanicalsoup.StatefulBrowser()
browser.set_user_agent(
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36')
a = browser.open('https://www.ebay-kleinanzeigen.de/m-einloggen.html')
browser.select_form('#login-form')
# browser.get_current_form().print_summary()
browser['loginMail'] = 'testaccount@8.dnsabr.com'
browser['password'] = 'testaccount'
browser.submit_selected()
选择列出产品的类别:
response = browser.open(
'https://www.ebay-kleinanzeigen.de/p-anzeige-aufgeben.html#?path=161/173/motorola_handy&isParent=false')
crsf = response.soup.find('input', {'name': '_csrf'})['value']
payload_select_category = {"_csrf": crsf,
"parentCategoryId": "210",
"categoryId": "223",
"attributeMap[autoteile_reifen.art_s]": "ersatz_reparaturteile"}
response = browser.post('https://www.ebay-kleinanzeigen.de/p-anzeige-aufgeben-schritt2.html', payload_select_category)
上传图片:
img_file = open(r'C:/Users/Oli/Google Drive/IMG_20190616_153432.jpg', 'rb')
payload_img_upload = {'name': 'Image.jpg'}
file = {'name': 'Image.jpg',
'file': ('Image.jpg', img_file, 'image/jpeg')}
response = browser.post('https://www.ebay-kleinanzeigen.de/p-bild-hochladen.html', payload_img_upload, files=file)
顺便说一句,我在这里提供的登录数据正在运行。
答案 0 :(得分:1)
尝试使用此代码:
header = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'}
header['content-type'] = 'text/html'
base_url = 'https://www.ebay-kleinanzeigen.de/'
session = requests.session()
res = BeautifulSoup(session.get(f'{base_url}m-einloggen.html').content, 'lxml')
csrf_token = res.find('input', {'name': '_csrf'})['value']
print(f'Found token: {csrf_token}')
payload = dict()
payload['targetUrl'] = '/'
payload['loginMail'] = 'testaccount@8.dnsabr.com'
payload['password'] = 'testaccount'
payload['_csrf'] = csrf_token
payload['fingerprint'] = '4dd6ba9367ddc34f0b00cf9566e01b63'
res = session.post(f'{base_url}m-einloggen.html', data=payload, allow_redirects=True)
print(f'Logged in: {res.status_code}')
img_file = open(r'C:/Users/Oli/Google Drive/IMG_20190616_153432.jpg', 'rb')
files = {'name': 'TestImg.jpg', 'file': ('TestImg.jpg', img_file, 'image/jpeg', {'Expires': '10'})}
header['content-type'] = 'multipart/form-data; boundary=----moxieboundary1563218428568'
header['x-csrf-token'] = csrf_token
res = session.post(f'{base_url}p-bild-hochladen.html',headers = header, files=files)
print(f'Image uploaded: {res.status_code}')
也有这种编码。图片以某种编码方式上传,您能告诉我这是哪种编码。我认为这是问题所在: