爬虫:https://www.cnblogs.com/wupeiqi/articles/5354900.html
详细连接:https://www.cnblogs.com/wupeiqi/articles/6283017.html
一、爬虫基本操作
1.1 爬虫
- 定向
- 非定向
1.2 步骤
- 下载页面
- 筛选:正则表达式
1.3 开源模块
- requests- response = request.get()
- response.txt
 
- beautifulsoup- bs = Beautifulsoup(request.txt)
- bs.find(id = “”)
 
二、requests库
Requests 是使用 Apache2 Licensed 许可证的 基于Python开发的HTTP 库,其在Python内置模块的基础上进行了高度的封装,从而使得Pythoner进行网络请求时,变得美好了许多,使用Requests可以轻而易举的完成浏览器可有的任何操作。
1.Get请求
| 1 | # 1、无参数实例 | 
2.POST请求
| 1 | # 1、基本POST实例 | 
总结
| 1 | import requests | 
详细
| 1 | def param_method_url(): | 
三、beautifulsoup4库
| 1 | from bs4 import BeautifulSoup | 
总结:
| 1 | soup = BeautifulSoup(response.text,feature = "html.parser") | 
自动登录
点击登录,若页面刷新,以form表单提交,若为刷新,以ajax提交
form提交和ajax提交https://blog.csdn.net/gghh2015/article/details/79116718
模块详细
| 1 | import requests | 
c. 模块详细使用
    requests
    
    - 方法关系
        requests.get(.....)
        requests.post(.....)
        requests.put(.....)
        requests.delete(.....)
        ...
        
        requests.request('POST'...)
    - 参数
        request.request
        - method:  提交方式
        - url:     提交地址
        - params:  在URL中传递的参数,GET 
            requests.request(
                method='GET',
                url= 'http://www.oldboyedu.com',
                params = {'k1':'v1','k2':'v2'}
            )
            # http://www.oldboyedu.com?k1=v1&k2=v2
        - data:    在请求体里传递的数据
        
            requests.request(
                method='POST',
                url= 'http://www.oldboyedu.com',
                params = {'k1':'v1','k2':'v2'},
                data = {'use':'alex','pwd': '123','x':[11,2,3]}  #data中不能有字典
            )
               #如果以data形式传递,request将数据封装为请求头和请求体
            请求头:
                content-type: application/url-form-encod..... 
            请求体:
                use=alex&pwd=123
                 - json   在请求体里传递的数据
            requests.request(
                method='POST',
                url= 'http://www.oldboyedu.com',
                params = {'k1':'v1','k2':'v2'},
                json = {'use':'alex','pwd': '123'}
            )
            请求头:
                content-type: application/json
            请求体:
                "{'use':'alex','pwd': '123'}"
            PS: 字典中嵌套字典时使用json,如果没有嵌套,用data即可
            
        - headers   请求头
        
            requests.request(
                method='POST',
                url= 'http://www.oldboyedu.com',
                params = {'k1':'v1','k2':'v2'},
                json = {'use':'alex','pwd': '123'},
                headers={
                    'Referer': 'http://dig.chouti.com/',  ##上一次访问的网站
                    'User-Agent': "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36"   ##客户端
                }
            )
         - cookies  Cookies 放到header中发送
                 
         - files    上传文件
         
         - auth      基本认知(headers中加入加密的用户名和密码)
         
         - timeout  请求和响应的超市时间
         
         - allow_redirects  是否允许重定向
         
         - proxies  代理
         
         - verify   是否忽略证书
         
         - cert      证书文件
         
         - stream   村长下大片
         
             - session: 用于保存客户端历史访问信息