Hansimov commited on
Commit
1db460d
1 Parent(s): e92817a

:gem: [Feature] New BatchWebpageContentExtractor: Extract webpage content from multiple html_paths concurrently

Browse files
documents/webpage_content_extractor.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import re
2
  from pathlib import Path
3
  from pprint import pprint
@@ -88,15 +89,48 @@ class WebpageContentExtractor:
88
  return markdown_str
89
 
90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
  if __name__ == "__main__":
92
- html_path = (
93
- Path(__file__).parents[1]
94
- / "files"
95
- / "urls"
96
- # / "stackoverflow.com_questions_295135_turn-a-string-into-a-valid-filename.html"
97
- # / "www.liaoxuefeng.com_wiki_1016959663602400_1017495723838528.html"
98
- # / "docs.python.org_zh-cn_3_tutorial_interpreter.html"
99
- / "zh.wikipedia.org_zh-hans_%E7%94%B0%E4%B8%AD%E6%9F%A0%E6%AA%AC.html"
 
 
 
 
100
  )
101
- extractor = WebpageContentExtractor()
102
- main_content = extractor.extract(html_path)
 
1
+ import concurrent.futures
2
  import re
3
  from pathlib import Path
4
  from pprint import pprint
 
89
  return markdown_str
90
 
91
 
92
+ class BatchWebpageContentExtractor:
93
+ def __init__(self) -> None:
94
+ self.html_path_and_extracted_content_list = []
95
+ self.done_count = 0
96
+
97
+ def extract_single_html(self, html_path):
98
+ webpage_content_extractor = WebpageContentExtractor()
99
+ extracted_content = webpage_content_extractor.extract(html_path)
100
+ self.html_path_and_extracted_content_list.append(
101
+ {"html_path": html_path, "extracted_content": extracted_content}
102
+ )
103
+ self.done_count += 1
104
+ logger.success(
105
+ f"> [{self.done_count}/{self.total_count}] Extracted: {html_path}"
106
+ )
107
+
108
+ def extract(self, html_paths):
109
+ self.html_path = html_paths
110
+ self.total_count = len(self.html_path)
111
+ with concurrent.futures.ThreadPoolExecutor() as executor:
112
+ futures = [
113
+ executor.submit(self.extract_single_html, html_path)
114
+ for html_path in self.html_path
115
+ ]
116
+ for idx, future in enumerate(concurrent.futures.as_completed(futures)):
117
+ result = future.result()
118
+
119
+ return self.html_path_and_extracted_content_list
120
+
121
+
122
  if __name__ == "__main__":
123
+ html_root = Path(__file__).parents[1] / "files" / "urls" / "python tutorials"
124
+ html_paths = [
125
+ html_root / html_filename
126
+ for html_filename in [
127
+ "docs.python.org_zh-cn_3_tutorial_interpreter.html",
128
+ "stackoverflow.com_questions_295135_turn-a-string-into-a-valid-filename.html",
129
+ "www.liaoxuefeng.com_wiki_1016959663602400_1017495723838528.html",
130
+ ]
131
+ ]
132
+ batch_webpage_content_extractor = BatchWebpageContentExtractor()
133
+ html_path_and_extracted_content_list = batch_webpage_content_extractor.extract(
134
+ html_paths
135
  )
136
+ # pprint(html_path_and_extracted_content_list)