1、requests之post请求
''' post请求访问github cookie有时间限制 请求URL: 请求方式:post 请求头referer:(上一个页面地址) user_agent: 请求体:(只有post有) ''' #访问login页获取token信息 ''' 请求URL: http://github.com/login 请求方式: get 响应头:set——cookie:(服务端反诬,告诉浏览器,要设置session) 请求头: cookie user—agent ''' import requests import re headers={...... } response=requests.get('dizhi',headers=headers) login_cookies=response.cookie.get_dict() token=re.findall('zhengzeguize',response.tex,re.S)[0] print(token) #找到第一个页面的token #登录 往session发送post请求 #请求的URL=http://github.com/session #携带请求头、请求体、login页的cookies信息 headers2={ 'referer':'', 'user-agent':'', #cookie } #拼接请求体 form={ 请求头里面的东西(其中authenticity_token:token)#上面获取第一个页面的token } res=requests.post('dizhi',data=form,headers=headers2,cookies=login_cookies)#访问第一个页面返回的cookies with open ('gui.html','w',encoding='utf-8') as f: f.write(res.text)
2、requests高级用法
#requests响应 # import requests # res=requests.get('dizhi') # res.content#获取二字流 # res.history#上一次跳转页面 # res.cookies#1、返回cookie字典2、返回cookie对象 # res.cookies.get_dict()#获取cookies信息转换成字典 # res.cookies.items()#获取cookies信息转换成字典 # res.elapsed#访问时间 #requests高级用法 #往地址发送get请求 url='' res=resquests.get(url,stream=True)#stream=True是将content设置为迭代对象 with open('in', 'wb') as f: for content in res.iter_content():#逐行读取 f.write(content) # 官网链接: http://docs.python-requests.org/en/master/user/advanced/#proxies # 代理设置:先发送请求给代理,然后由代理帮忙发送(封ip是常见的事情) import requests proxies={ # 带用户名密码的代理,@符号前是用户名与密码 'http':'http://tank:123@localhost:9527', 'http':'http://localhost:9527', 'https':'https://localhost:9527', } response=requests.get('https://www.12306.cn', proxies=proxies) print(response.status_code) # 支持socks代理,安装:pip install requests[socks] import requests proxies = { 'http': 'socks5://user:pass@host:port', 'https': 'socks5://user:pass@host:port' } respone=requests.get('https://www.12306.cn', proxies=proxies) print(respone.status_code) 复制代码 复制代码 ''' 爬取西刺免费代理: 1.访问西刺免费代理页面 2.通过re模块解析并提取所有代理 3.通过ip测试网站对爬取的代理进行测试 4.若test_ip函数抛出异常代表代理作废,否则代理有效 5.利用有效的代理进行代理测试 <tr class="odd"> <td class="country"><img src="//fs.xicidaili.com/images/flag/cn.png" alt="Cn"></td> <td>112.85.131.99</td> <td>9999</td> <td> <a href="/2019-05-09/jiangsu">江苏南通</a> </td> <td class="country">高匿</td> <td>HTTPS</td> <td class="country"> <div title="0.144秒" class="bar"> <div class="bar_inner fast" style="width:88%"> </div> </div> </td> <td class="country"> <div title="0.028秒" class="bar"> <div class="bar_inner fast" style="width:97%"> </div> </div> </td> <td>6天</td> <td>19-05-16 11:20</td> </tr> re: <tr class="odd">(.*?)</td>.*?<td>(.*?)</td> ''' import requests import re import time HEADERS = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36', } def get_index(url): time.sleep(1) response = requests.get(url, headers=HEADERS) return response def parse_index(text): ip_list = re.findall('<tr class="odd">.*?<td>(.*?)</td>.*?<td>(.*?)</td>', text, re.S) for ip_port in ip_list: ip = ':'.join(ip_port) yield ip def test_ip(ip): print('测试ip: %s' % ip) try: proxies = { 'https': ip } # ip测试网站 ip_url = 'https://www.ipip.net/' # 使用有效与无效的代理对ip测试站点进行访问,若返回的结果为200则代表当前测试ip正常 response = requests.get(ip_url, headers=HEADERS, proxies=proxies, timeout=1) if response.status_code == 200: return ip # 若ip代理无效则抛出异常 except Exception as e: print(e) # 使用代理爬取nba def spider_nba(good_ip): url = 'https://china.nba.com/' proxies = { 'https': good_ip } response = requests.get(url, headers=HEADERS, proxies=proxies) print(response.status_code) print(response.text) if __name__ == '__main__': base_url = 'https://www.xicidaili.com/nn/{}' for line in range(1, 3677): ip_url = base_url.format(line) response = get_index(ip_url) ip_list = parse_index(response.text) for ip in ip_list: # print(ip) good_ip = test_ip(ip) if good_ip: # 真是代理,开始测试 spider_nba(good_ip)
3、selenium模块
#selenium ''' 什么是selenium? 最初是一个自动化测试工具,可以使用他帮我们 驱动浏览器自动去执行某些自定义好的操作。例如在页面中执行JS代码、跳过登录验证 为什么要用它? 优点 使用requests模块登录需要分析大量的复杂通信流程,使用selenium 可以轻松跳过登录验证 缺点 浏览器会加载CSS、JS、图片、视频。。。。数据, 爬虫效率相比于requests模块较低 如何用? 下载selenium地址 #pip install -i清华园地址 ''' ''' from selenium import webdriver # 用来驱动浏览器的 调用得到一个动作连对象,破解滑动验证 from selenium.webdriver import ActionChains # 破解滑动验证码的时候用的 可以拖动图片 from selenium.webdriver.common.by import By # 按照什么方式查找属性,By.ID,By.CSS_SELECTOR from selenium.webdriver.common.keys import Keys # 键盘按键操作 from selenium.webdriver.support import expected_conditions as EC # 和下面WebDriverWait一起用expected_conditions的别名EC from selenium.webdriver.support.wait import WebDriverWait # 等待页面加载某些元素 ''' # from selenium import webdriver # import time # #1、通过谷歌浏览器驱动打开谷歌浏览器 # ch = webdriver.Chrome(r'E:\python\chromedriver_win32\chromedriver.exe')#绝对路径 # # #向某一个网页发送get请求 # ch.get('https://www.cnblogs.com/kermitjam/p/10863922.html') # time.sleep(100) #2、放在python解释器的script里面 要配置路径 # ch = webdriver.Chrome() # try:#若try出现异常 # ch.get('https://www.cnblogs.com/kermitjam/p/10863922.html') # time.sleep(100) # finally:#无论发生什么都会关闭浏览器 # ch.close() #一百度为例 # from selenium.webdriver.support.wait import WebDriverWait # from selenium import webdriver # import time # #1、通过谷歌浏览器驱动打开谷歌浏览器 # ch = webdriver.Chrome(r'E:\python\chromedriver_win32\chromedriver.exe')#绝对路径 # wait=WebDriverWait(ch,10) #参数一:驱动对象,参数二:等待时间 # #向某一个网页发送get请求 # #1、访问百度 # ch.get('https://www.baidu.com/') # #2\查找input输入框 # wait.until(EC.presence_of_element_located(By.ID,'kw'))#参数一查找属性,参数二属性名字 # #3、搜索一拳超人 # input_tag.send_keys('一拳超人') # #4、按键盘会车 # input_tag.send_keys(Keys.ENTER) # time.sleep(100) #京东网站 from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.wait import WebDriverWait from selenium import webdriver import time #1、通过谷歌浏览器驱动打开谷歌浏览器 ch = webdriver.Chrome(r'E:\python\chromedriver_win32\chromedriver.exe')#绝对路径 wait=WebDriverWait(ch,10) #参数一:驱动对象,参数二:等待时间 #向某一个网页发送get请求 #1、访问百度 ch.get('https://www.jd.com/') #2\查找input输入框 input_tag=wait.until(EC.presence_of_element_located((By.ID,'key')))#参数一查找属性,参数二属性名字 #3、搜索一拳超人 input_tag.send_keys('唐诗三百首') #4、根据class属性名称查找标签 search_button=wait.until(EC.presence_of_element_located((By.CLASS_NAME,'button'))) #点击搜索按钮 search_button.click() time.sleep(100)
4、万能破解登录
# from selenium import webdriver # import time # ch = webdriver.Chrome(r'E:\python\chromedriver_win32\chromedriver.exe') # # try: # #wait = WebDriverWait(ch, 10) #显示等待:可以等待某个元素加载 # #隐式等待,等待页面所有元素加载 # ch.get('https://china.nba.com/') # ch.implicitly_wait(10) # news=ch.find_element_by_class_name('nav-news') # #获取标签对象 # print(news) # #获取标签的名字 # print(news.tag_name) # time.sleep(10) ''' ===============所有方法=================== element是查找一个标签 elements是查找所有标签 1、find_element_by_link_text 通过链接文本去找 2、find_element_by_id 通过id去找 3、find_element_by_class_name 4、find_element_by_partial_link_text 5、find_element_by_name 6、find_element_by_css_selector 7、find_element_by_tag_name ''' #选择器 from selenium import webdriver import time driver = webdriver.Chrome(r'E:\python\chromedriver_win32\chromedriver.exe') try: driver.get('http://baidu.com') driver.implicitly_wait(10) #find_element_by_link_text 通过链接文本去找 #根据登录 # send_tag=driver.find_element_by_link_text('登录') # send_tag.click() # find_element_by_partial_link_text 通过局部文本查找a标签 # login_button=driver.find_element_by_partial_link_text('登') # login_button.click() #find_element_by_class_name 根据class属性名查找 login_tag=driver.find_element_by_class_name('') login_tag.click() # find_element_by_name 根据name属性查找 username=driver.find_element_by_name('') username.send_keys('') #用户名 #find_element_by_id 通过ID属性名查找 password = driver.find_element_by_id('') password.send_keys('')#用户密码 #find_element_by_css_selector 根据属性选择器查找 login_submit=driver.find_element_by_css_selector('') #driver.find_element_by_css_selector('.pass-button-submit') login_submit.click() #find_element_by_tag_name 根据标签名称查找标签 div=driver.find_elements_by_tag_name('div') print(div.tag_name) time.sleep(10) finally: driver.close()
'' 爬取快代理: 1.访问快代理页面 2.通过re模块解析并提取所有代理 3.通过ip测试网站对爬取的代理进行测试 4.若test_ip函数抛出异常代表代理作废,否则代理有效 5.利用有效的代理进行代理测试 <tr> <td data-title="IP">124.205.143.212</td> <td data-title="PORT">40585</td> <td data-title="匿名度">高匿名</td> <td data-title="类型">HTTP</td> <td data-title="位置">北京市北京市 鹏博士宽带</td> <td data-title="响应速度">2秒</td> <td data-title="最后验证时间">2019-06-17 16:30:54</td> </tr> re: <tr>.*?<td data-title="IP">(.*?)</td>.*?<td data-title="PORT">(.*?)</td> ''' ''' 页面链接 第一页: https://www.kuaidaili.com/free/ 第二页: https://www.kuaidaili.com/free/inha/2/ ''' import requests import re import time headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36', } def get_index(url): time.sleep(1) response1 = requests.get(url, headers=headers) return response1 def parse_index(text): ip_list1 = re.findall('<tr>.*?<td data-title="IP">(.*?)</td>.*?<td data-title="PORT">(.*?)</td>', text, re.S) for ip_port in ip_list1: ip1 = ':'.join(ip_port) yield ip1 def test_ip(ip2): print('测试ip: %s' % ip2) try: proxies = {'https': ip2} # ip测试网站 ip_url1 = 'https://www.ipip.net/' # 使用有效与无效的代理对ip测试站点进行访问,若返回的结果为200则代表当前测试ip正常 response2 = requests.get(ip_url1, headers=headers, proxies=proxies, timeout=1) if response2.status_code == 200: return ip # 若ip代理无效则抛出异常 except Exception as e: print(e) # 使用代理爬取nba def spider_nba(good_ip1): url = 'https://china.nba.com/' proxies = {'https': good_ip1} response3 = requests.get(url, headers=headers, proxies=proxies) print(response3.status_code) print(response3.text) if __name__ == '__main__': base_url = 'https://www.kuaidaili.com/free/inha/{}/' for line in range(1, 2905): ip_url = base_url.format(line) response = get_index(ip_url) ip_list = parse_index(response.text) for ip in ip_list: good_ip = test_ip(ip) if good_ip: spider_nba(good_ip)
from selenium import webdriver import time #获取驱动对象 driver = webdriver.Chrome() try: #自动登陆抽屉新热榜 #发送get请求 driver.get('https://dig.chouti.com/ ') #隐式等待 driver.implicitly_wait(10) #获取 '登陆' 按钮 send_tag = driver.find_element_by_id('login_btn') send_tag.click() #获取手机号输入框 username = driver.find_element_by_class_name('login-phone') username.send_keys('***********') time.sleep(1) #获取密码输入框 password = driver.find_element_by_class_name('pwd-password-input') password.send_keys('***********') time.sleep(1) #获取 '登陆' 按钮 login = driver.find_elements_by_link_text('登录') login[1].click() time.sleep(10) finally: driver.close()
小结:我觉得tank这么帅,这么优秀,应该会给我优的,对吧