路飞学城—python爬虫实战密训-—第1章

一.本节学习体会,心得:

  1. 因为之前只是看了Python基础的内容,以为爬虫的内容自己会看不懂,但是视频里的老师讲得很好,生动易懂。
  2. 学习编程,就要亲自练习,看视频觉得懂了,自己亲自做起来,就有了一点阻碍。

二.知识点总结:

  1. 爬虫定义:通过程序去获取网页上自己想要的数据,也就是自动抓取数据。爬虫通过分析html代码,从中获取文本,图片,视频.....
  2. 发送请求用http request,在这个歌之前要安装requests 和bs4 模块,在控制台窗口里用 pip install 模块名  来进行安装。
  3. request请求中包含,请求头,请求体,method:url:params:data:json:headers:cookies。
  4. 爬取汽车之家新闻练习
    import requests
    from bs4 import BeautifulSoup
    ret = requests.get(url="https://www.autohome.com.cn/news/")
    ret.encoding = ret.apparent_encoding
    # print(ret.text)
    soup = BeautifulSoup(ret.text,'html.parser')
    div = soup.find(name='div',id='auto-channel-lazyload-article')
    li_list = div.find_all(name='li')
    for li in li_list:
        h3 = li.find(name='h3')
        if not h3:
            continue
       
    
        p = li.find(name='p')
      
    
        a = li.find(name='a')
      
    
        img = li.find('img')
        src = img.get('src')
    
        file_name = src.rsplit('__',1)[1]
        print(file_name)
        ret_img = requests.get(
            url= 'https:'+src
        )
        with open(file_name,'wb') as f:
            f.write(ret_img.content)

猜你喜欢

转载自www.cnblogs.com/andydong/p/9265831.html
今日推荐