• 爬虫来啦!Day91


    # 一.爬虫
    # 1.基本操作
    # 排名爬虫刷票
    # 抽屉网的所有发布新闻点赞
    # 自动化程序模拟用于的日常操作
    # 投票的机制是利用cookies,禁用cookies模式
    # 自定义的异步IO模块就是Socket的客户端
    # 基本操作:使用python登陆任何的网站,图片识别验证码比较困难,需要额外的图片识别或特殊api(伪造浏览器的任何行为)
    # 2.性能相关的操作
    # 用最短的时间:多线程多进程解决大并发操作
    # 5个线程并发,共同等待有损耗,把5个线程等待时进行其他程序
    # 并发方案:用异步IO,之前的并发采用5个线程,现采用一个线程 gevent/Twisted/asyncio/aiohttp傻瓜式封装调用方法
    # 会用方法表现异步IO
    # IO多路复用 Select
    # 3.做一个属于自己的爬虫框架,以高性能的方式爬取网站
    # 打包开源放在github上,知名的爬虫框架:Scrapy:封装了所有的爬虫常用的方法,具有异步IO的并发功能(Twisted)
    # 给予Scrapy源码定义自定义框架
    # 使用Scrapy框架
    # 二.Tornado 异步非阻塞特性框架
    # 1.Tornado的基本使用,分析源码功能,可以快速基于Tornado的单表的增删改查。小示例感受
    # 2.Toanado的源码剖析
    # 3.异步非阻塞的就是Soctek的服务端
    ###################################################################################################################
    #爬虫的基本知识
    # 什么是爬虫?
    # 互联网最开始根本没有百度、Google、Bing等,如果想要访问网站只能记住域名,很久之前是大黄页,一本书包含了各自域名的地址
    # 想要访问网站就必须用文档记录找到文档检查,这样是十分麻烦的,文档内容多会很麻烦,于是诞生了搜索引擎,
    # 通过自动程序抓取网站。做搜索引擎的来做大黄页,通过客户需求找到,客户提交信息至搜索引擎,收集信息给提供客户
    # 百度通过机器人抓取关键字,提取信息。
    # 网络信息都是息息相关的,友情链接到别的网站,机器人爬取网站所有的<a>标签,肯定有超链接,实际就是一个关系网的互相联系
    # 把爬虫放在一个网站,自动化程序任意查找,具有定向和非定向
    # 定向:只爬取某一个或某几个指定网站
    # 非定向:爬取很多内容
    # 公众号通过找网络上的合适资源爬取信息
    # 租房信息的爬取
    # 请求:http请求本质是一个字符串,包裹在整个的HTML的页面里的所有的标签内的信息,找到想要的标签信息(写组合正则表达式),
    # python不用重复的写正则表达式,有开源的模块(类库)帮助实现
    # 1.request模块访问网站
    # pip3 install requests
    # request.get('http:www.baidu.com')
    # response.text
    # 2.beautisoup模块
    # pip3 install Beautisoup4
    # from bs4 import Beautisoup
    # soup = Beautisoup(response.text)这个HTML被转换为对象,对象可以嵌套对象,HTML的父子关系(对象嵌套)
    # target = soup.find(id = 'auto-channel-lazyload-article')标签嵌套对象,找对象
    # print (target)
    ###########################################################################################################
    # 小结:
    # 需求一:
    # response = request.get('url')
    # response.text
    # response.content
    # response.encoding
    # response.aparent_encoding
    # response.status_code
    # soup = beautifulsoup('HTML')
    # soup.find('div)
    # soup = find(id='i1')
    # v1 = soup = find('div, id = 'i1')两个参数可以作为组合条件
    # v2 = soup = find_all('div, id = 'i1')参数与find相同,返回的内容是列表了
    # v1是对象,v2是列表
    # v1.text直接拿到文本 v2[0].text
    # 需求二:
    # 通过脚本程序自动登陆GitHup[其他网站]
    # requests,访问网站需要输入用户名和密码,页面刷新使用form表单,没刷新使用AJAX提交
    # post_dict = {
    # "phone":'111111',
    # "password": 'xxx',
    # 'oneMonth':1
    # }
    # response = request.post(
    # url = 'https://dig.chouti.com/login',
    # data = post_dict
    # )
    #print(response.text)
    # cookie_dict = response.cookies.get_dict()
    ###########################################################################################################
    # 1.基本的模块的使用
    # 2.模拟登陆
    # 3.各个网站的套路不同
    ###########################################################################################################
    # 三、模块的详细使用
    # requests
    # -方法关系:
    # rquests.get(..)
    # rquests.post(..)
    # rquests.put(..)
    # rquests.delete(..)
    ######以上都是request方法
    # ——参数
    # rquests.rquest:
    # -method:提交方式
    # -url:提交地址
    # -params:在url上传递的参数放入GET,如果访问的是www.baidu.com,如果params = {'k1':'v1'}
    # requests.request(
    # method = 'GET',
    # url = 'www.baidu.com',
    # params = {'k1':'v1','k2':'v2'}
    # )
    # 提交后运行的结果为:http://www.baidu.com?k1=v1&k2=v2
    # -data:在请求体里传递的数据,可以是字典、字节或文件对象
    # requests.request(
    # method = 'POST',
    # url = 'www.baidu.com',
    # params = {'k1':'v1','k2':'v2'},
    # data = {'use':'xxx','pwd':'123'},或者data = 'user=xxx&pwd=123123'
    # )
    # -json:在请求体里传递数据,会把参数json.dump变成整体的字符串传递(字典中嵌套字典时使用)
    # requests.request(
    # method = 'GET',
    # url = 'www.baidu.com',
    # json = {'use':'xxx','pwd':'123'},>>>>请求头:content-typ:application/json;请求体:"{'use':'xxx','pwd':'123'}"
    # )
    # ######Django会根据发送的请求头的内容判断是否需要将请求体的内容进行POST的json转换
    # - headers:请求头,爬取各种网站需要附加请求头
    # 手写一个上一次访问的网站 headers = {
    # Referer:'http;//www.baidu.com'
    # 'User-Agent'>>可以伪造任何浏览器形式,来自什么设备
    # }
    # 网址参考:http://www.cnblogs.com/wupeiqi/articles/6283017.html
    # -cookies:Cookies
    # cookies是放在请求头里的
    ###########################################################################################################
    # -files:上传文件'name':文件名(文件对象)
    # requests.post(
    # url='xxx',
    # files = {
    # 'f1':open('s1.py','rb'),
    # 'f1':('上传至服务器端的文件名',open('s1.py','rb))
    # }
    # )
    ######
    # -auth:认证,加密放在请求头里
    # -timeout:超时,两个时间>>1.发送的时间;2.等待的时间
    # -allow_redirects 是否允许重定向
    # -proxies:代理,开发投票的网站只能使用同一个ip,不介入代理的话是不能修改的
    # ret = requests.post(
    # url='https://dig.chouti.com/link/vote?linksId=20162281',#某一个新闻的点赞url
    # cookies = {'gpsd':'601f5ba78f2a5fdb1d374817117eca80'},#得到的gpsd
    # #HTTPS记得加请求头呦
    # headers = {'User-Agent':
    # 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)'
    # ' Chrome/66.0.3359.181 Safari/537.36'},
    # proxys = {
    # 'http':'http://4.19.128.5:8099'代理服务器,代理方发送
    # }
    # )
    ######
    # -stream:流式传输(一点一点下载文件)循环式下载 for i in r.iter_content()
    # -verify:
    # -cert:
    import requests
    from bs4 import BeautifulSoup
    response = requests.get(url='https://www.autohome.com.cn/news/')
    # response.encoding = 'gbk'
    # 下载的过程中是字节,必须用它的方式发来后再反解
    # 用原网页的编码方式反解:response.encoding = response.apparent_enconding
    response.encoding = response.apparent_encoding  #等于response.encoding = 'gbk'
    # print(response.text)
    soup = BeautifulSoup(response.text, features='html.parser')
    #将文本转换为对象,features表示以什么内置的处理方式转换:1.lxml;2.html.parser
    # lxml性能更好
    target = soup.find(id = 'auto-channel-lazyload-article')
    # print(target)
    # 继续缩小范围去找
    li_list = target.find_all('li')
    # find_all找到的是一个列表的汇总,本身已经没有soup对象的方法了,需要再用方法需要用list_all[0]拿到列表的某一个值
    # print(li_list)
    for i in li_list:
        a = i.find('a')
        if a:
            print('http:'+a.attrs.get('href'))#attrs表示找到对象的所有的属性,href就是标签的属性
            txt = a.find('h3') #拿到的h3是什么类型?print显示的是字符串,实际是对象,打印显示的是对象,需要加文本信息
            print(txt.text)
            img_url = 'http:'+a.find('img').attrs.get('src')#小练习:拿到a标签下的img标签的src属性
            print(img_url)
            img_response = requests.get(url=img_url)
            import uuid
            file_name = str(uuid.uuid4()) + '.jpg'
            with open(file_name,'wb') as f:
                f.write(img_response.content)#content意思是以字节的形式保存文件
    import requests
    # from bs4 import BeautifulSoup
    # url = "https://huan.moe/"
    # response = requests.get(url)
    # response.encoding = response.apparent_encoding
    # # print(response.text)
    # soup = BeautifulSoup(response.text, features='html.parser')
    # target = soup.find(attrs={'class','g-desktop-1-4 g-tablet-1-2 g-phone-1-2 archive-general card general type-post status-publish format-standard hentry'})
    # print(target)
    post_dict = {
      "phone":'*******',
       "password": '******',
        'oneMonth':1
    }
    response = requests.post(
          url = 'https://dig.chouti.com/login',
           data = post_dict,
            )
    
    print(response.text)
    cookie_dict = response.cookies.get_dict()
    print(cookie_dict)
    res = requests.get(
        url='https://dig.chouti.com/profile',
    
    
    
    
    )
    print(res.text)
    import requests
    #抽屉网gpsd验证点赞法
    ret = requests.post(
        url='https://dig.chouti.com/link/vote?linksId=20162281',#某一个新闻的点赞url
        cookies = {'gpsd':'601f5ba78f2a5fdb1d374817117eca80'},#得到的gpsd
        #HTTPS记得加请求头呦
       
    )
    print(ret.text)
    抽屉网点赞方法
    import requests
    r1 = requests.get('https://dig.chouti.com/',
                     )
    r1_cookies = r1.cookies.get_dict()
    print(r1_cookies)
    
    post_dict = {
      "phone":'llllllll',
       "password": 'llllllll',
        'oneMonth':1
    }
    r2 = requests.post(
          url = 'https://dig.chouti.com/login',
           data = post_dict,
           
            cookies = r1_cookies,
            )
    print(r2.text)
    # r2_cookies = r2.cookies.get_dict()
    # print(r2_cookies)
    # r2_cookies = r2.cookies.get_dict()
    # print(r2.text)
    # cookie_dict = r2.cookies.get_dict()
    # print(cookie_dict)
    # res = requests.get(
    #     url='https://dig.chouti.com/profile',
    #   
    #
    # )
    # print(res.text)
    r3 = requests.post(
        # url='https://dig.chouti.com/profile',
        url='https://dig.chouti.com/link/vote?linksId=20160191',
       
        cookies = {'gpsd':r1_cookies.get('gpsd')}
    )
    print(r1_cookies.get('gpsd'))
    print(r3.text)
    抽屉网组合拳(有点小心机)
    Win a contest, win a challenge
  • 相关阅读:
    表单元素(控件)不可见属性“visibility”和“display”分析
    如何实现网页的右键菜单功能
    Google Chrome 11 浏览器 下Flash Debug 插件无效的解决办法
    embed区别object
    在Fedora 12 下安装Intel X4500 显卡驱动
    HDOJ 2000
    费马小定理
    HDOJ 2055
    HDOJ 1018(阶乘位数)
    大数阶乘所得数位数的定理
  • 原文地址:https://www.cnblogs.com/pandaboy1123/p/9164767.html
Copyright © 2020-2023  润新知