File size: 1,790 Bytes
6cf0820
 
 
e448a74
f9c42cf
af2c647
6cf0820
 
 
 
 
 
 
 
f9c42cf
6cf0820
8bf48d8
e448a74
6cf0820
af2c647
6cf0820
 
 
 
 
 
 
e448a74
f9c42cf
 
 
 
e448a74
6cf0820
cf4c3f8
6cf0820
cf4c3f8
 
 
 
 
 
 
8bf48d8
6cf0820
 
 
 
e448a74
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import requests
from pathlib import Path
from utils.enver import enver
from utils.logger import logger
from networks.filepath_converter import QueryToFilepathConverter
from networks.network_configs import REQUESTS_HEADERS


class GoogleSearcher:
    # https://github.com/Nv7-GitHub/googlesearch/blob/master/googlesearch/__init__.py
    def __init__(self):
        self.url = "https://www.google.com/search"
        self.enver = enver
        self.enver.set_envs(proxies=True)
        self.filepath_converter = QueryToFilepathConverter()

    def send_request(self, result_num=10, safe=False):
        self.request_response = requests.get(
            url=self.url,
            headers=REQUESTS_HEADERS,
            params={
                "q": self.query,
                "num": result_num,
            },
            proxies=self.enver.requests_proxies,
        )

    def save_response(self):
        if not self.output_path.exists():
            self.output_path.parent.mkdir(parents=True, exist_ok=True)
        logger.note(f"Saving to: [{self.output_path}]")
        with open(self.output_path, "wb") as wf:
            wf.write(self.request_response.content)

    def search(self, query, result_num=10, safe=False, overwrite=False):
        self.query = query
        self.output_path = self.filepath_converter.convert(self.query)
        logger.note(f"Searching: [{self.query}]")
        if self.output_path.exists() and not overwrite:
            logger.success(f"HTML existed: {self.output_path}")
        else:
            self.send_request(result_num=result_num, safe=safe)
            self.save_response()
        return self.output_path


if __name__ == "__main__":
    searcher = GoogleSearcher()
    # searcher.search("python教程")
    searcher.search("python tutorials")