Skip to main content

爬虫的一些工具。

Project description

download_tools

download_tools 下载工具库,也包括爬虫的一些工具。

安装

Required

pip install requests selenium

安装 download_tools

pip install download_tools

使用

下载文件

下载文件时,会先检查存储目录是否有同名文件,有就不下载。

download_url()

默认只使用requests获取,也可以selenium,需要浏览器firefox和Firefox驱动。

import download_tools as dt

dt.download_url('https://www.baidu.com', r'F:\test') 

# 修改名称和增加后缀
dt.download_url('https://www.baidu.com', save_dir=r'F:\test', name='baidu',suffix='html') 
def download_url(url, save_dir, name=None,  suffix=None, 
                headers=None ,  type='get', post_data=None,
                has_selenium='no', selenium_sleep_time=0, headless=True, sleep_time=0):
    """
    通过链接下载文件,文件名存在时不保存。
    Parameters
    ----------
    url : str
        链接
    save_dir : str
        保存的文件夹路径
    name : str
        文件名,默认None为url文件名
    suffix : str
        文件后缀,默认'txt'
    sleep_time : int
        下载一次休眠的秒数,默认0
    has_selenium : str {'both','only','no'}
        'no'不使用selenium, 'both'先requests不行再selenium,'only'只使用selenium
    selenium_sleep_time : int
        使用selenium时,等待页面加载的休眠时间,默认0
    Returns
    -------
    None
    Examples
    --------
    >>> download_url('https://www.baidu.com',r'F:\test') 
    """    

download_str()

适用于已经获取到源代码,直接保存。当name参数为空时,使用当前日期时间做文件名。

import download_tools as dt

page_souce = '<p>测试</p>' dt.download_str(page_souce, r'F:\test', name='测试', suffix='html')
dt.download_str(page_souce, save_dir=r'F:\test')

headers

生成headers

import download_tools as dt

# 随机生成header
dt.Headers().get()
# 随机生成windows平台chrome浏览器headers
dt.Headers(os='win', browser="chrome", headers=True).get()

for i in range(10):
    print(dt.Headers().get())

dtools.fake_headers fork from https://pypi.org/project/fake-headers/
修改:generate()函数名修改为get()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

download_tools-0.0.4.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

download_tools-0.0.4-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file download_tools-0.0.4.tar.gz.

File metadata

  • Download URL: download_tools-0.0.4.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.8

File hashes

Hashes for download_tools-0.0.4.tar.gz
Algorithm Hash digest
SHA256 3c7dc3d33168eb447c3055570abf7112ce7d023c521b2a19d2b56fe1f2d30182
MD5 7e790d09fb9ce2918f6319ef671c1c9a
BLAKE2b-256 a51a2c297b564e586ad90f26ae44381fc0a9db32539c3100dca1fa95fbd4423b

See more details on using hashes here.

File details

Details for the file download_tools-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: download_tools-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 8.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.8

File hashes

Hashes for download_tools-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 66a01ecc0a0bb448b9b40fdc758c2dc9bd0633732c6eaa6f9de9982d2f25dad8
MD5 5742525a8da93400b6459b69d37c036f
BLAKE2b-256 6e962bc75e2e7936ffa62f110535a1f9f647f63360bbd41185a00aed4e8ef938

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page