""" requests """ # import requests # reponse = requests.get("https://www.baidu.com") # print(type(reponse)) # print(reponse.status_code) # print(type(reponse.text)) # print(reponse.text) # print(reponse.cookies) """ GET請求 params=data """ # import requests # data = { # "name": "dc", # "age": 20 # } # reponse = requests.get("http://httpbin.org/get", params=data) # print(reponse.text) # print(type(reponse.text)) # print(reponse.json()) # print(type(reponse.json())) """ 抓取頁面 """ # import requests # import re # headers = { # "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/17.17134" # } # reponse = requests.get("https://www.zhihu.com/explore", headers=headers) # pattern = re.compile("explore-feed.*?question_link.*?>(.*?)</a>", re.S) # titles = re.findall(pattern, reponse.text) # print(titles) """ 抓取二進位制檔案(以GitHub的站點圖示為例) """ # import requests # r = requests.get("https://github.com/favicon.ico") # with open('favicon.ico', 'wb') as f: # f.write(r.content) """ POST請求 """ # import requests # data = { # "user": "dc", # "password":123456 # } # r = requests.post("http://httpbin.org/post", data=data) # print(r.text) """ requests.codes """ # import requests # r = requests.get("https://jianshu.com") # print("yes") if not r.status_code == requests.codes.ok else print("Request Successfully") """ 檔案上傳 """ # import requests # files = { # "file": open('favicon.ico', 'rb') # } # r = requests.post("http://httpbin.org/post", files=files) # print(r.text) """ Cookies """ # import requests # r = requests.get("https://www.baidu.com") # print(r.cookies) # for key, value in r.cookies.items(): # print(key + "=" + value) """ 用cookies來維持登陸狀態(兩種方法) """ # import requests # headers = { # 'cookie':'_zap=22772871-d4c0-49d0-a164-06bfb3f5baa6; d_c0="ABCow7lbdQ6PTgpWk2Esu7cq8Cvv8Kgy06w=|1541173950"; __gads=ID=167b99930ab194ba:T=1547351508:S=ALNI_MZ5MgHhlPvmrH8Wfz0Wcndr8f4BfA; z_c0="2|1:0|10:1547351520|4:z_c0|92:Mi4xSFBrSkJBQUFBQUFBRUtqRHVWdDFEaVlBQUFCZ0FsVk40QU1vWFFBbVFtZlNLSmljS2tVVXZPRXFzZUl2eHhMMmdB|b4914639e2423d24c604a243727600fff0e6d88af07fb022b2cf1d1501d038b0"; tgw_l7_route=8ffa4a0b7ecd9bdb5ad19b8c1037b063; q_c1=3439401ba6af416e87c3f7d6e5593b07|1551170696000|1541173952000; _xsrf=a49f7c0b12c1cebf58db38497b95164f; __utma=51854390.649426157.1551170703.1551170703.1551170703.1; __utmb=51854390.0.10.1551170703; __utmc=51854390; __utmz=51854390.1551170703.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utmv=51854390.100-1|2=registration_date=20170202=1^3=entry_date=20170202=1; _xsrf=sBADPkXpPYitokYZRn2lBQorlVlWlWCM', # 'Host': 'www.zhihu.com', # 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36' # } # r = requests.get("https://www.zhihu.com", headers=headers) # print(r.text) # import requests # cookie = '_zap=22772871-d4c0-49d0-a164-06bfb3f5baa6; d_c0="ABCow7lbdQ6PTgpWk2Esu7cq8Cvv8Kgy06w=|1541173950"; __gads=ID=167b99930ab194ba:T=1547351508:S=ALNI_MZ5MgHhlPvmrH8Wfz0Wcndr8f4BfA; z_c0="2|1:0|10:1547351520|4:z_c0|92:Mi4xSFBrSkJBQUFBQUFBRUtqRHVWdDFEaVlBQUFCZ0FsVk40QU1vWFFBbVFtZlNLSmljS2tVVXZPRXFzZUl2eHhMMmdB|b4914639e2423d24c604a243727600fff0e6d88af07fb022b2cf1d1501d038b0"; tgw_l7_route=8ffa4a0b7ecd9bdb5ad19b8c1037b063; q_c1=3439401ba6af416e87c3f7d6e5593b07|1551170696000|1541173952000; _xsrf=a49f7c0b12c1cebf58db38497b95164f; __utma=51854390.649426157.1551170703.1551170703.1551170703.1; __utmb=51854390.0.10.1551170703; __utmc=51854390; __utmz=51854390.1551170703.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utmv=51854390.100-1|2=registration_date=20170202=1^3=entry_date=20170202=1; _xsrf=sBADPkXpPYitokYZRn2lBQorlVlWlWCM' # jar = requests.cookies.RequestsCookieJar() # headers = { # 'Host': 'www.zhihu.com', # 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36' # } # for cookie in cookie.split(";"): # key, value = cookie.split("=", 1) # jar.set(key, value) # r = requests.get("https://www.zhihu.com", cookies=jar, headers=headers) # print(r.text) """ 會話維持 """ # import requests # S = requests.session() # S.get("http://httpbin.org/cookies/set/number/123456789") # r = S.get('http://httpbin.org/cookies') # print(r.text) """ SSL證照驗證 """ # import requests # r = requests.get("https://www.12306.cn", verify=False) # print(r.status_code) """ 代理設定 """ # import requests # proxies = { # 'http': 'http://10.10.1.10.3128', # 'http':'http://10.10.1.10.3100' # } # r = requests.get("http://www.baidu.com", proxies=proxies) # print(r.text) """ 超時設定 """ # import requests # r = requests.get('http://localhost:5000', auth=('username', 'password')) # print(r.status_code) """ Prepared Request """ # from requests import Request, Session # url = 'http://httpbin.org/post' # data = { # "name":"germey" # } # headers = { # 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36' # } # s = Session() # req = Request('POST', url, data=data, headers=headers) # prepped = s.prepare_request(req) # r = s.send(prepped) # print(r.text)