1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| from requests.adapters import HTTPAdapter headers = dict() headers["User-Agent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36" headers["Accept"] = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" headers["Accept-Encoding"] = "gzip, deflate, sdch" headers["Accept-Language"] = "zh-CN,zh;q=0.8" headers["Accept-Language"] = "zh-CN,zh;q=0.8" request_retry = HTTPAdapter(max_retries=3)
def my_get(url, refer=None): session = requests.session() session.headers = headers if refer: headers["Referer"] = refer session.mount('https://', request_retry) session.mount('http://', request_retry) return session.get(url)
|