python会话请求图像下载-禁止访问403

时间:2019-01-06 14:20:06

标签: python image python-requests session-cookies urllib

我正在尝试从网址下载图像,并使用python请求。会话还添加了用户代理,仍然面临403禁止的错误,请帮忙。

我的代码:

import requests
from bs4 import BeautifulSoup
import pandas as pd
s = requests.session()
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}



#logged in! cookies saved for future requests.
r=s.get("https://example.com/homepage",headers=headers)
#cookies sent automatically!

soup=BeautifulSoup(r.text,'html.parser')
te=s.get("https://www.example.com/"+soup.find(class_='yes').find('a').get('href'),headers=headers).text

s.get('https://img.example.com/exampleimgcode.jpg', stream = True,headers=headers}))

out[]: <Response [403]>

0 个答案:

没有答案