File size: 1,711 Bytes
6cf0820
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
52
53
54
55
56
import requests
from pathlib import Path
from utils.enver import enver


class RequestHeaderConstructor:
    def __init__(self):
        self.construct()

    def construct(self):
        self.headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.62",
        }


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.output_root = Path(__file__).parents[1] / "files"

    def send_request(self, query, result_num=10):
        res = requests.get(
            url=self.url,
            headers=RequestHeaderConstructor().headers,
            params={
                "q": self.query,
                "num": result_num,
                # "hl": "en",
                # "start": 0,
            },
            proxies=self.enver.requests_proxies,
        )
        return res

    def save_response(self, res, query):
        output_filename = query.replace(" ", "_") + ".html"
        if not self.output_root.exists():
            self.output_root.mkdir(parents=True, exist_ok=True)
        output_path = self.output_root / output_filename
        with open(output_path, "wb") as wf:
            wf.write(res.content)

    def search(self, query):
        self.query = query
        res = self.send_request(query)
        self.save_response(res, query)


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