-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcray.py
More file actions
162 lines (127 loc) · 5.56 KB
/
cray.py
File metadata and controls
162 lines (127 loc) · 5.56 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
"""Beautiful Soup Web scraper."""
import logging
from typing import Any, Callable, Dict, List, Optional, Tuple
from urllib.parse import urljoin
from gpt_index.readers.base import BaseReader
from gpt_index.readers.schema.base import Document
logger = logging.getLogger(__name__)
def _substack_reader(soup: Any) -> Tuple[str, Dict[str, Any]]:
"""Extract text from Substack blog post."""
extra_info = {
"Title of this Substack post": soup.select_one("h1.post-title").getText(),
"Subtitle": soup.select_one("h3.subtitle").getText(),
"Author": soup.select_one("span.byline-names").getText(),
}
text = soup.select_one("div.available-content").getText()
return text, extra_info
def _readthedocs_reader(soup: Any, url: str) -> Tuple[str, Dict[str, Any]]:
"""Extract text from a ReadTheDocs documentation site"""
links = soup.find_all("a", {"class": "reference internal"})
rtd_links = []
for link in links:
rtd_links.append(link["href"])
for i in range(len(rtd_links)):
if not rtd_links[i].startswith("http"):
rtd_links[i] = urljoin(url, rtd_links[i])
texts = []
import requests
from bs4 import BeautifulSoup
for doc_link in rtd_links:
page_link = requests.get(doc_link)
soup = BeautifulSoup(page_link.text, "html.parser")
try:
text = soup.find(attrs={"role": "main"}).get_text()
except IndexError:
text = None
if text:
texts.append("\n".join([t for t in text.split("\n") if t]))
return "\n".join(texts), {}
def _readmedocs_reader(soup: Any, url: str,include_url_in_text: bool = True) -> Tuple[str, Dict[str, Any]]:
"""Extract text from a ReadMe documentation site"""
import logging
import requests
from bs4 import BeautifulSoup
links = soup.find_all("a")
docs_links = [link["href"] for link in links if "/docs/" in link["href"]]
docs_links = list(set(docs_links))
for i in range(len(docs_links)):
if not docs_links[i].startswith("http"):
docs_links[i] = urljoin(url, docs_links[i])
docs_text = []
for i, doc_link in enumerate(docs_links):
try:
page_link = requests.get(doc_link)
soup = BeautifulSoup(page_link.text, "html.parser")
title = soup.title.string.strip() if soup.title else "No title available"
text = ""
for element in soup.find_all("main", {"class": "layout__main"}):
for child in element.descendants:
if child.name == "a" and child.has_attr("href"):
if include_url_in_text:
url = child.get("href")
if url is not None and "edit" in url:
text += child.text
else:
text += f"{child.text} (Reference url: {doc_link}{url}) "
elif child.string and child.string.strip():
text += child.string.strip() + " "
docs_text.append({'id': i+1, 'page_link': doc_link, 'title': title, 'text': text})
except Exception as e:
logging.error(f"Could not extract text from {doc_link}: {e}")
continue
return f"{docs_text}", {}
# return f"{' '.join([doc['text'] for doc in docs_text])}", {}
DEFAULT_WEBSITE_EXTRACTOR: Dict[
str, Callable[[Any, str], Tuple[str, Dict[str, Any]]]
] = {
"substack.com": _substack_reader,
"readthedocs.io": _readthedocs_reader,
"readme.com": _readmedocs_reader,
}
class BeautifulSoupWebReader(BaseReader):
"""BeautifulSoup web page reader.
Reads pages from the web.
Requires the `bs4` and `urllib` packages.
Args:
website_extractor (Optional[Dict[str, Callable]]): A mapping of website
hostname (e.g. google.com) to a function that specifies how to
extract text from the BeautifulSoup obj. See DEFAULT_WEBSITE_EXTRACTOR.
"""
def __init__(
self,
website_extractor: Optional[Dict[str, Callable]] = None,
) -> None:
"""Initialize with parameters."""
self.website_extractor = website_extractor or DEFAULT_WEBSITE_EXTRACTOR
def load_data(
self, urls: List[str], custom_hostname: Optional[str] = None,include_url_in_text: Optional[bool] = True
) -> List[Document]:
"""Load data from the urls.
Args:
urls (List[str]): List of URLs to scrape.
custom_hostname (Optional[str]): Force a certain hostname in the case
a website is displayed under custom URLs (e.g. Substack blogs)
Returns:
List[Document]: List of documents.
"""
from urllib.parse import urlparse
import requests
from bs4 import BeautifulSoup
documents = []
for url in urls:
try:
page = requests.get(url)
except Exception:
raise ValueError(f"One of the inputs is not a valid url: {url}")
hostname = custom_hostname or urlparse(url).hostname or ""
soup = BeautifulSoup(page.content, "html.parser")
data = ""
extra_info = {"URL": url}
if hostname in self.website_extractor:
data, metadata = self.website_extractor[hostname](soup, url,include_url_in_text)
# print(metadata)
extra_info.update(metadata)
else:
data = soup.get_text()
documents.append(Document(data, extra_info=extra_info))
return documents