Hansimov commited on
Commit
b259fec
β€’
1 Parent(s): 64a0dbf

:gem: [Feature] New HTMLFetcher: download url to local html file

Browse files
networks/filepath_converter.py CHANGED
@@ -71,7 +71,7 @@ class FilepathConverter:
71
  self.filename = filename
72
  self.filepath = filepath
73
 
74
- return {"filename": self.filename, "filepath": self.filepath}
75
 
76
 
77
  class UrlToFilepathConverter(FilepathConverter):
@@ -93,12 +93,12 @@ class QueryToFilepathConverter(FilepathConverter):
93
  if __name__ == "__main__":
94
  query = "python 教程"
95
  query_converter = QueryToFilepathConverter()
96
- print(query_converter.convert(query)["filename"])
97
 
98
  # url = "https://trafilatura.readthedocs.io/en/latest/quickstart.html"
99
- url = "https://www.bing.com/search?q=Bing+AI&showconv=1&setlang=en&cc=us"
100
- # url = (
101
- # "https://stackoverflow.com/questions/295135/turn-a-string-into-a-valid-filename"
102
- # )
103
  url_converter = UrlToFilepathConverter()
104
- print(url_converter.convert(url, parent=query)["filepath"])
 
71
  self.filename = filename
72
  self.filepath = filepath
73
 
74
+ return self.filepath
75
 
76
 
77
  class UrlToFilepathConverter(FilepathConverter):
 
93
  if __name__ == "__main__":
94
  query = "python 教程"
95
  query_converter = QueryToFilepathConverter()
96
+ print(query_converter.convert(query))
97
 
98
  # url = "https://trafilatura.readthedocs.io/en/latest/quickstart.html"
99
+ url = (
100
+ "https://stackoverflow.com/questions/295135/turn-a-string-into-a-valid-filename"
101
+ )
102
+
103
  url_converter = UrlToFilepathConverter()
104
+ print(url_converter.convert(url, parent=query))
networks/html_fetcher.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ from pathlib import Path
3
+ from utils.enver import enver
4
+ from utils.logger import logger
5
+ from networks.filepath_converter import UrlToFilepathConverter
6
+
7
+
8
+ class HTMLFetcher:
9
+ def __init__(self):
10
+ self.enver = enver
11
+ self.enver.set_envs(proxies=True)
12
+
13
+ def send_request(self):
14
+ logger.note(f"Fetching: [{self.url}]")
15
+ self.request_response = requests.get(
16
+ url=self.url,
17
+ headers={
18
+ "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",
19
+ },
20
+ proxies=self.enver.requests_proxies,
21
+ )
22
+
23
+ def save_response(self):
24
+ self.save_path = UrlToFilepathConverter().convert(self.url)
25
+ if not self.save_path.exists():
26
+ self.save_path.parent.mkdir(parents=True, exist_ok=True)
27
+
28
+ logger.success(f"Saving to: [{self.save_path}]")
29
+
30
+ with open(self.save_path, "wb") as wf:
31
+ wf.write(self.request_response.content)
32
+
33
+ def fetch(self, url):
34
+ self.url = url
35
+ self.send_request()
36
+ self.save_response()
37
+
38
+
39
+ if __name__ == "__main__":
40
+ url = (
41
+ "https://stackoverflow.com/questions/295135/turn-a-string-into-a-valid-filename"
42
+ )
43
+ fetcher = HTMLFetcher()
44
+ fetcher.fetch(url)