zoukankan      html  css  js  c++  java
  • Python爬虫的步骤和工具

    #四个步骤

    1.查看crawl内容的源码格式          crawl的内容可以是 url(链接),文字,图片,视频

    2.请求网页源码        (可能要设置)代理,限速,cookie

    3.匹配            用正则表达式匹配

    4.保存数据          文件操作

    #两个基本工具(库)

    1.urllib

    2.requests

    #使用reuests库的一个例子,抓取可爱图片

    import requests  #导入库
    import re
                       
    url =r'https://www.woyaogexing.com/tupian/keai'   #链接
    response =requests.get(url)                                   #get()函数,得到网页
    response.encoding ='utf-8'          #让源码中的中文正常显示
    html =response.text             #加载网页源码
    strs ='<div class="txList_1 .">.*?src="(.*?)".*?>'   #正则表达式
    patern =re.compile(strs,re.S)         #封装成对象,以便多次使用
    items =re.findall(patern,html)         #匹配
    for i in  items:
        with open('%d.jpg'%items.index(i),'wb') as file: #新建文件,以二进制写形式'wb'
            url ='https:'+i
            file.write(requests.get(url).content)    #写入数据,图片是二进制数据
  • 相关阅读:
    zyUpload+struct2完成文件上传
    js表单动态添加数据并提交
    Hibernate注解
    ueditor的配置和使用
    设计模式
    静态Include和动态Include测试并总结
    java笔试题
    perf使用示例1
    perf 简介
    ss简单使用
  • 原文地址:https://www.cnblogs.com/vvlj/p/9580423.html
Copyright © 2011-2022 走看看