File size: 1,360 Bytes
fa1be5f
 
 
 
 
 
 
 
 
 
bd6cfa0
fa1be5f
 
 
 
 
 
 
 
 
 
 
 
bd6cfa0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa1be5f
 
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
import re
import requests
from bs4 import BeautifulSoup

class SearchResult:
	def __init__(self, results: list[str], user_agent: str, did_you_mean: str = "", tailored_query: str = ""):
		self.results = results
		self.user_agent = user_agent
		self.suggestion_query = did_you_mean
		self.tailored_query = tailored_query

	def parse_results(self):
		results = self.results[1:]
		headers = {
			"User-Agent": self.user_agent
		}

		stripped_pages: list[{
			"page_title": str,
			"text_content": str
		}] = [] # type: ignore

		for link_entry in results:
			if len(stripped_pages) < 1:
				twitter_pattern = re.compile(r".*twitter.*", re.IGNORECASE)

				if not re.search(twitter_pattern, link_entry):
					text_content = ""
					response = requests.get(link_entry, headers=headers)
					soup = BeautifulSoup(response.text, "html.parser")

					title = soup.title.string or "No title provided" # type: ignore
					relevant_tags = ["p", "li", "h1", "h2", "h3", "h4", "h5", "h6"]

					for tag in relevant_tags:
						elements = soup.find_all(tag, class_=lambda c: c != 'ads' and c != 'header' and c != 'footer')
						for element in elements:
							if element.text.strip().lower():
								text_content += element.text.strip() + '\n'

					stripped_pages.append({ "page_title": title, "text_content": text_content })
			else:
				continue

		return stripped_pages