hero image

augwewe

keep hungry keep foolish

python基础知识
project detailed description
link name
link detailed description
book name
Detailed description of the book
article name
Detailed description of the article
延迟插件

cava原创...小于 1 分钟notebook爬虫技术
存储网页所有图片

爬虫技术-存储网页所有图片

import re, requests,cchardet,urllib.parse,traceback
from urllib.parse import urlparse
from urllib.parse import urljoin
from requests.exceptions import RequestException,ProxyError,SSLError

class IMGSAVE(object):
    def __init__(self,url):
        self.url = url

    def ask_url(self,url,headers=None,timeout=10,binary=False,debug=False):
        if "https" not in url:
            url = "https://"+url
        global status,html,redirected_url
        the_headers = {
        "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36"
    }
        if headers:
            the_headers = headers
        try:
            response=requests.get(url,headers=the_headers,timeout=timeout)
            if response.status_code == 200:
                if binary:
                    html = response.content
                else:
                    encoding = cchardet.detect(response.content)['encoding']
                    html = response.content.decode(encoding)
                status = response.status_code
                redirected_url = response.url

        except(RequestException,ProxyError,SSLError)as e:
            print(e)
            if debug:
                traceback.print_exc()
            msg = 'Failed downled:{}'.format(url)
            print(msg)
            if binary:
                html = b''
            else:
                html = ''
            status=0
        return status,html,redirected_url

    def regex(self,html):
        pattern = re.compile('<img.*?src="(.*?)".*?>',re.S)
        result = re.findall(pattern,html)
        print(result)
        return result

    def save_img(self,filename,content):
        with open(filename,"wb")as fb:
            fb.write(content)

    def suffix(self,path):
        suffix=[".png",".jpg",".jpge"] #可以添加任意后缀到列表中
        for i in suffix:
            if i not in path.lower():
                continue
            return  i
    def start(self,url):
        status, html, redirected_url = self.ask_url(url=url, debug=True)
        template = "{scheme}://{netloc}" 
        the_parse = urlparse(redirected_url)
        url = template.format(scheme=the_parse.scheme, netloc=the_parse.netloc)  # 指定
        print(url)
        r_u = self.regex(html)  # 同下方解析中的path
        if not r_u:
            pass
        img_urls = [urljoin(url, img) for img in r_u]
        # print(img_urls)
        for index, url in enumerate(img_urls):
            postffix = self.suffix(url)
            filename = f'{index}{postffix}'
            content = self.ask_url(url, debug=True, binary=True)[1]
            self.save_img(filename, content)
def main():
    url="www.bbiquge.net"
    work=IMGSAVE(url)
    work.start(url)
if __name__ == '__main__':
    main()


cava原创...大约 1 分钟notebook爬虫技术
pymongo

MongoDB-pymongo

导入库 import pymongo

1. 连接数据库

client=pymongo.MongoClient(host="localhost",port=27017)
client=pymongo.MongoClient('mongodb://localhost:27017/')

cava原创...大约 2 分钟notebook爬虫技术MongoDB
MongoDB教程 6.0版

MongoDB 6.0教程

1.1下载MongoDB

https://www.mongodb.com/try/download/community

下载对应的版本

选custom可自定义安装路径,complete默认路径

后续点击next,,选择不勾选(Install MongoDB Compass),直至install,勾选后下载安装速度会较慢些。


cava原创...大约 1 分钟notebook爬虫技术
Django

Django

为什么要用虚拟环境?

在一台电脑上,想开发多个不同的项目, 需要用到同一个包的不同版本, 如果使用上面的命令, 在同一个目录下安装或者更新, 新版本会覆盖以前的版本, 其它的项目就无法运行了。

在使用django开发项目的时候,一个环境只能对应一个项目,因为这样可以方便管理第三方包,每个项目使用的第三方可能不一样,若不安装虚拟环境、都装在系统里面,每次项目加载都需要加载所有的安装包,影响效率。

1.Django环境搭建

创建文件夹‘MY_Web'

进入此文件夹路径下输入以下命令


cava原创...大约 1 分钟notebookpython Web框架
request

cava原创...大约 2 分钟notebook爬虫技术
2
3