def __init__(self, url, cookies2=None, proxies=None, cookies=None): self.url = url self.proxies = proxies self.email = SendEmail() # cookies = cookies2 if cookies2 else self._get_cookies() self.session = requests.Session() self.session.cookies = cookiejar_from_dict(cookies) # session设置cookie headers = { 'Accept': 'application/json, text/javascript, */*; q=0.01', 'Accept-Encoding': 'gzip, deflate, br', 'Accept-Language': 'zh-CN,zh;q=0.9', 'Connection': 'keep-alive', 'Host': 'antirobot.tianyancha.com', 'Cookies': cookies, 'Referer': 'https://antirobot.tianyancha.com/captcha/verify?return_url={}&rnd=' .format(self.url), 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36', 'X-CSRFToken': 'null', 'X-Requested-With': 'XMLHttpRequest', }
def prepare_request(self, request): """Constructs a :class:`PreparedRequest <PreparedRequest>` for transmission and returns it. The :class:`PreparedRequest` has settings merged from the :class:`Request <Request>` instance and those of the :class:`Session`. :param request: :class:`Request` instance to prepare with this session's settings. :rtype: requests.PreparedRequest """ cookies = request.cookies or {} # Bootstrap CookieJar. if not isinstance(cookies, cookielib.CookieJar): cookies = cookiejar_from_dict(cookies) # Merge with session cookies merged_cookies = merge_cookies( merge_cookies(RequestsCookieJar(), self.cookies), cookies) # Set environment's basic authentication if not explicitly set. auth = request.auth if self.trust_env and not auth and not self.auth: auth = get_netrc_auth(request.url) p = CuPreparedRequest() p.prepare( method=request.method.upper(), url=request.url, files=request.files, data=request.data, json=request.json, headers=merge_setting(request.headers, self.headers, dict_class=CaseInsensitiveDict), params=merge_setting(request.params, self.params), auth=merge_setting(auth, self.auth), cookies=merged_cookies, hooks=merge_hooks(request.hooks, self.hooks), ) return p
def __init__(self, num): self.num = num self.session = requests.session() self.session.cookies = cookiejar_from_dict(DEFAULT_COOKIES[0]) self.headers = DEFAULT_HEADERS self.tel = datas[num].get("phone", 0)
# 获取HMSACCOUNT的cookie a = session.get( f'https://hm.baidu.com/hm.gif?cc=1&ck=1&cl=24-bit&ds=1536x864&vl=410&ep=14806%2C4421&et=3&ja=0&ln=zh-cn&lo=0<={str(int(datetime.now().timestamp() * 1000))}&rnd=966120243&si=e92c8d65d92d534b0fc290df538b4758&su=https%3A%2F%2Fwww.tianyancha.com%2F&v=1.2.51&lv=2&sn=16695' ) # print(a.cookies) session.headers = headers # 第一次请求 # 获取aliyun的cookie resp = session.get('https://www.tianyancha.com/') resp.encoding = 'UTF-8' # print(resp.cookies) # 自己添加cookie cookie = {'ssuid': str(suid), 'bannerFlag': 'undefined'} cookies = cookiejar_from_dict(cookie) session.cookies.update(cookies) params1 = {'ps': '40', '_': str(int(datetime.now().timestamp() * 1000))} # 获取csrftoken,TYCID,undefined的cookie resp4 = session.get( f'https://www.tianyancha.com/user/getHomeNavHeader.html?_={str(int(datetime.now().timestamp() * 1000))}' ) # print(resp4.cookies) headers = { 'Host': 'hm.baidu.com', 'Connection': 'keep-alive', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36',
from datetime import datetime import json import requests from requests.sessions import cookiejar_from_dict session = requests.Session() def download(url): return session.get(url) if __name__ == '__main__': url = 'https://www.tianyancha.com/' resp = download(url) resp.encoding = 'UTF-8' url1 = f'https://www.tianyancha.com/claim/getLabels.json?_={int(datetime.now().timestamp() * 1000)}' resp1 = session.get(url1) print(resp1.text) # uri = f'https://www.tianyancha.com/user/getHomeNavHeader.html?_={int(datetime.now().timestamp() * 1000)}' # resp2 = session.get(uri) cookie = {'bannerFlag': 'undefined'} cookies = cookiejar_from_dict(cookie) session.cookies.update(cookies) resp3 = session.get('https://www.baidu.com') print(resp3)
def __init__(self): cookies = self._get_cookies() self.session = requests.Session() self.session.cookies = cookiejar_from_dict(cookies) # session设置cookie
'X-Requested-With': 'XMLHttpRequest', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36', 'Content-Type': 'application/json; charset=UTF-8', 'Referer': 'https://www.tianyancha.com/', 'Accept-Encoding': 'gzip, deflate, br', 'Accept-Language': 'zh-CN,zh;q=0.9' } session.get('https://www.tianyancha.com/') session.cookies.update( cookiejar_from_dict({ 'ssuid': '5385309444', 'bannerFlag': 'undefined', 'Hm_lpvt_e92c8d65d92d534b0fc290df538b4758': str(int(datetime.now().timestamp() * 1000)), 'Hm_lvt_e92c8d65d92d534b0fc290df538b4758': str(int(datetime.now().timestamp() * 1000)) })) session.get( f'https://www.tianyancha.com/newest.html?ps=40&_={str(int(datetime.now().timestamp() * 1000))}' ) t1 = int(datetime.now().timestamp() * 1000) session.get( f'https://www.tianyancha.com/tongji/1562724106551.json?_={str(int(datetime.now().timestamp() * 1000))}' ) # 第一次请求
import requests from PIL import Image from lxml import etree from requests.sessions import cookiejar_from_dict cookie = {} with open('cookies.txt', 'r') as f: result = json.load(f) for c in result: name = c.get('name') value = c.get('value') cookie[name] = value session = requests.Session() session.cookies = cookiejar_from_dict(cookie) resp = session.get('https://www.tianyancha.com/search?base=bj') print(resp.text) html = etree.HTML(resp.text) url_lists = html.xpath('//div[@class="result-list sv-search-container"]/div') print(url_lists) url = f"http://antirobot.tianyancha.com/captcha/getCaptcha.json?t={str(int(datetime.now().timestamp() * 1000))}" data = session.get(url) print(data.json()) bgImage = data.json().get('data').get('bgImage') targetImage = data.json().get('data').get('targetImage') print(base64.urlsafe_b64decode(bgImage))