Skip to main content

该库是来自 digipodium 的实用程序库

项目描述

一个 python 库,可用于从文件、pdf、doc(x) 文件中提取数据,以及将数据保存到这些文件中。该库也可用于从网站中抓取和提取网页数据。

安装要求和说明

应安装 Python 3.8 或更高版本。之后打开您的终端: 对于 Windows 用户:

pip install dputils

对于 Mac/Linux 用户:

pip3 install dputils

文件模块

dputils.files 中的函数:

  1. 获取数据:

    • 要导入,请使用语句:
      from dputils.files import get_data
      
    • 从以 args 给出的任何扩展名的文件中获取数据(目前支持文本文件、二进制文件、pdf、doc,更多即将到来!)
    • 示例调用:
      content = get_data(r"sample.docx")
      print(content)
      
    • 根据输出参数返回字符串或二进制数据
    • 图像不会被提取
  2. 保存数据:

    • 要导入,请使用语句:
      from dputils.files import save_data
      
    • save_data 可用于将数据写入和保存到有效扩展名的文件中。
    • 示例调用:
      pdfContent = save_data("sample.pdf", "Sample text to insert")
      print(pdfContent)
      
    • 如果文件被成功访问和修改,则返回 True。否则为假。

爬取模块

dputils.scrape 中的函数:

  1. 获取网页数据:

    • 要导入,请使用语句:
      from dputils.scrape import get_webpage_data
      
    • get_webpage_data 可用于从任何网站以 BeautifulSoup 对象的形式获取数据
    • 示例调用:
      soup = get_webpage_data("https://en.wikipedia.org/wiki/Hurricane_Leslie_(2018)")
      print(type(soup))
      
    • 以 BeautifulSoup 对象的形式返回数据
  2. 提取一个:

    • extract_one 可用于从给定 BeautifulSoup 对象中的数据中提取数据项作为字典
    • 要导入,请使用语句:
      from dputils.scrape import extract_one
      
    • 用法:
      soup = get_webpage_data("https://en.wikipedia.org/wiki/Hurricane_Leslie_(2018)")
      
      dataDict = extract_one(soup, title = {'tag' : 'h1', 'attrs' : {'id' : 'firstHeading'}, 'output' : 'text'})
      print(dataDict)
      
    • 输出将是 dict 类型
    example here
    
  3. extract_many:

    导入函数

    from dputils.scrape import extract_many, get_webpage_data
    

    抓住你的汤

    url = "https://www.flipkart.com/search?q=mobiles&otracker=search&otracker1=search&marketplace=FLIPKART&as-show=on&as=off"
    soup = get_webpage_data(url)
    

    提供 dict 中的所有参数,如下例所示。

    target = {
    'tag': 'div',
    'attrs':{'class':'_1YokD2 _3Mn1Gg'}
    }
    items = {
        'tag': 'div',
        'attrs':{'class':'_1AtVbE col-12-12'}
    }
    title = {
        'tag': 'div',
        'attrs':{'class':'_4rR01T'}
    }
    price = {
        'tag': 'div',
        'attrs':{'class':'_30jeq3 _1_WHN1'}
    }
    rating = {
        'tag': 'div',
        'attrs':{'class':'_3LWZlK'}
    }
    link = {
        'tag': 'a',
        'attrs':{'class':'_1fQZEK'},
        'output':'href'
    }
    

    调用具有正确名称的函数

    • :来自 get_webpage_data() 函数
    • target:内容所在的小节(可选)
    • items:包含项目的重复 HTML 代码(必需)
    • 其他将是要提取的项目的名称和字典,只需链接提取一
    out= extract_many_1(soup, target=target, items=items, title=title, price=price, rating=rating, link=link)
    
    • 输出将是一个字典列表
    print(out)
    

    (可选)将数据转换为 pandas 数据框

    import pandas as pd
    df = pd.DataFrame(out)
    print(df)
    
  4. 提取网址

    • extract_urls 可用于从给定 BeautifulSoup 对象中的数据中提取所有 url 作为列表
    • 要导入,请使用语句:
      from dputils.scrape import extract_urls
      
    • 用法:
      soup = get_webpage_data("https://en.wikipedia.org/wiki/Hurricane_Leslie_(2018)")
      
      urlList = extract_urls(soup, target = {'tag' : 'div', 'attrs' : {'class':'s-matching-dir sg-col-16-of-20 sg-col sg-col-8-of-12 sg-col-12-of-16'}})
      print(urlList)
      
    • 输出将是 url 列表

这些函数可以在 python 3.8 或更高版本上使用。

更多帮助参考:https ://github.com/digipodium/dputils

感谢您使用 dputils!

项目详情


下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

dputils-0.1.16.2.tar.gz (8.5 kB 查看哈希

已上传 source

内置分布

dputils-0.1.16.2-py3-none-any.whl (8.2 kB 查看哈希

已上传 py3