zoukankan      html  css  js  c++  java
  • 路飞学成-Python爬虫实战密训-第1章

    1,本节学习体会、心得 :

      经过几个月的努力学完了django。因此才选择了这个爬虫课程。经过第一章的学习,再次体会到了python的强大。之前一直为正则发愁,每次都要修改好几次,才能匹配到。严重影响效率。然而,在这节中学到了新的技能 beautifulsoup4模块。用起来太爽了,简直就像是在写jquery 。大大提高了匹配的效率。

    武老师讲的非常通俗易懂,但是如果只听的话,过后就忘了。在去写已经学过的代码,仍然不知道怎么写。但是照着笔记,举一反三的去爬取几个站之后。再来写的话就可以抛弃笔记了。哈哈 ,也算是一点小心得了。

    2,本节的知识点总结:

      一、爬虫入门

      安装requests模块 :pip3 install requests  

      安装bs4 模块 :pip3 install beautifulstoup4 

      导入request模块: import requests

      导入bs4模块 :from  bst import BeautifulSoup    

    import requests
    from  bst import BeautifulSoup
    
    # 通过get方式获取要爬取的页面的内容 
    ret = requests.get(url='https://www.autohome.com.cn/news/')
    
    # 给获取到的内容设置编码  (apparent_encoding 获取当前内容的编码)
    ret.encoding = ret.apparent_encoding
     
    # 用beautifulsoup模块解析 获取到的内容
    soup = ret.Beautifulsoup(ret.text,'html.parser')  #html.parser 是解析的方式
     
    # find 找第一个对象  find_all 找到所有的对象 返回一个对象列表
    div = soup.find(name='div',id="auto-channel-lazyload-article")
    # div = soup.find(name='div',attrs=['id':"auto-channel-lazyload-article",'class':'btn'])
    # div.text
    # div.attrs
    # div.get('href')
    # 获取所有的li标签对象 find_all
    list = div.find_all(name='li')
    #list = div.find_all(name='li',_class='li')
    # list = div.find_all(name='li',attrs=['href':'xxx.xxx.com'])
    #遍历list对象列表 打印出每个li下的h3标签里的内容 a标签的href属性值  p标签的内容
    for i in list:
      h3 = i.find(name='h3')
      a = i.find(name='a')
      try:  #由于h3的可能会是空 print会报错 这里可以用if判断跳出循环 这里我用try不让它报错
        print(h3.text,a.get('href'))
        print(i.find('p').text)
      except:
        pass
    二、利用requests模拟登陆抽屉
    需要带上headers请求头 以及cookie
    #先获取第一次的未授权的cookie
    re1 = requests.get(
        url = 'https://dig.chouti.com/',
        headers = {
            "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36"},
    )
    # 拿到未授权的cookie 赋值给一个变量
    ck_dict1 = re1.cookies.get_dict()
    
    #post方式登陆 并带上未授权的cookie 进行认证登陆
    res_login = requests.post(
        url='https://dig.chouti.com/login',
        data={
            'phone':'8613500000000',
            'password':'abcd12345678',
            'oneMonth':1
        },
        headers={"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36"},
        cookies=ck_dict1
    )
    # print(res.text)
    #  带上认证过的cookie 进行点赞操作点赞
    ck_dict = res_login.cookies.get_dict()
    res1 = requests.post(
        url = 'https://dig.chouti.com/link/vote?linksId=20646261',
        headers = {"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36"},
        cookies = ck_dict1
    )
    print(res1)


      

       

      

      

  • 相关阅读:
    hdoj_1556Color the ball
    wchar_t与char转换(总结)
    算法艺术——网络最大流
    poj_3268Silver Cow Party
    poj_2352Stars
    BellmanFord模板
    saas模式
    什么是管道
    什么是CMMI
    saas模式
  • 原文地址:https://www.cnblogs.com/iamdi/p/9270620.html
Copyright © 2011-2022 走看看