-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrefs_html_collection.py
276 lines (233 loc) · 11.2 KB
/
refs_html_collection.py
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import logging
from typing import Dict, Any, List
import yaml
import requests
import time
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import pandas as pd
# Set up logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
def load_config(config_path: str) -> Dict[str, Any]:
with open(config_path, 'r') as file:
return yaml.safe_load(file)
class HTMLFetcher:
HTTP_ERROR_MESSAGES = {
400: "Bad Request",
401: "Unauthorized",
403: "Forbidden",
404: "Not Found",
408: "Request Timeout",
429: "Too Many Requests",
500: "Internal Server Error",
502: "Bad Gateway",
503: "Service Unavailable",
504: "Gateway Timeout"
}
def __init__(self, config_path: str = 'config.yaml'):
"""Initialize HTMLFetcher with configuration"""
self.config = load_config(config_path)
self.fetching_driver = self.config.get('html_fetching', {}).get('fetching_driver', 'requests')
self.batch_size = self.config.get('html_fetching', {}).get('batch_size', 20)
self.delay = self.config.get('html_fetching', {}).get('delay', 1.0)
self.timeout = self.config.get('html_fetching', {}).get('timeout', 50)
self.headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
}
def get_error_message(self, status_code: int) -> str:
"""Get descriptive error message for HTTP status code"""
return self.HTTP_ERROR_MESSAGES.get(status_code, "Unknown Error")
def fetch_html_with_requests(self, url: str) -> str:
"""Fetch HTML content using requests library"""
try:
response = requests.get(
url,
timeout=self.timeout,
headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'}
)
response.raise_for_status()
return response.text
except Exception as e:
logging.error(f"Error fetching {url}: {e}")
return f"Error: {str(e)}"
def fetch_html_with_selenium(self, url: str) -> str:
"""Fetch HTML content using selenium"""
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-dev-shm-usage")
try:
with webdriver.Chrome(options=chrome_options) as driver:
driver.set_page_load_timeout(self.timeout)
driver.get(url)
time.sleep(1) # Short delay to ensure page loads
return driver.page_source
except Exception as e:
logging.error(f"Selenium error for {url}: {e}")
return f"Error: {str(e)}"
def fetch_all_html(self, url_df: pd.DataFrame, parser_result: Dict) -> pd.DataFrame:
"""
Fetch HTML for all URLs in the DataFrame and add metadata from parser_result
"""
result_df = url_df.copy()
result_df['html'] = None
result_df['status'] = None
result_df['lang'] = None
result_df['fetch_timestamp'] = None
for i, (idx, row) in enumerate(result_df.iterrows()):
if i > 0 and i % self.batch_size == 0:
time.sleep(self.delay)
try:
fetch_start_time = pd.Timestamp.now()
if self.fetching_driver == 'selenium':
html = self.fetch_html_with_selenium(row['url'])
status = 200 if not html.startswith('Error:') else 500
else:
response = requests.get(
row['url'],
timeout=self.timeout,
headers=self.headers
)
status = response.status_code
if status == 200:
html = response.text
else:
error_msg = self.get_error_message(status)
html = f"Error: HTTP {status} - {error_msg}"
result_df.at[idx, 'status'] = status
result_df.at[idx, 'html'] = html
result_df.at[idx, 'fetch_timestamp'] = fetch_start_time
if status == 200:
try:
soup = BeautifulSoup(html, 'lxml')
lang = soup.html.get('lang', '')
if not lang:
meta_lang = soup.find('meta', attrs={'http-equiv': 'content-language'})
if meta_lang:
lang = meta_lang.get('content', '')
result_df.at[idx, 'lang'] = lang if lang else None
except Exception as e:
logging.error(f"Error detecting language for {row['url']}: {e}")
result_df.at[idx, 'lang'] = None
logging.info(f"Successfully fetched HTML for {row['url']} (Status: {status}, Time: {fetch_start_time})")
except requests.exceptions.HTTPError as e:
status = e.response.status_code
error_msg = self.get_error_message(status)
result_df.at[idx, 'status'] = status
result_df.at[idx, 'html'] = f"Error: HTTP {status} - {error_msg} - {str(e)}"
result_df.at[idx, 'lang'] = None
logging.error(f"HTTP error for {row['url']}: {status} ({error_msg})")
result_df.at[idx, 'fetch_timestamp'] = pd.Timestamp.now()
except requests.exceptions.Timeout as e:
result_df.at[idx, 'status'] = 408
result_df.at[idx, 'html'] = f"Error: HTTP 408 - Request Timeout - {str(e)}"
result_df.at[idx, 'lang'] = None
logging.error(f"Timeout error for {row['url']}: {e}")
result_df.at[idx, 'fetch_timestamp'] = pd.Timestamp.now()
except Exception as e:
result_df.at[idx, 'status'] = 500
result_df.at[idx, 'html'] = f"Error: HTTP 500 - Internal Server Error - {str(e)}"
result_df.at[idx, 'lang'] = None
logging.error(f"Failed to fetch HTML for {row['url']}: {e}")
result_df.at[idx, 'fetch_timestamp'] = pd.Timestamp.now()
# After fetching HTML, add metadata from parser_result
claims_with_refs = parser_result['claims'].merge(
parser_result['claims_refs'],
on='claim_id',
how='inner'
)
result_df = result_df.merge(
claims_with_refs[['claim_id', 'entity_id', 'entity_label',
'property_id', 'datavalue', 'reference_id']],
on='reference_id',
how='left'
)
# Extract object_id and object_label from datavalue
def extract_object_id(datavalue: str) -> str:
try:
value_dict = eval(datavalue)
if 'type' in value_dict:
if value_dict['type'] == 'wikibase-entityid':
# Entity type handling
if 'numeric-id' in value_dict['value']:
return f"Q{value_dict['value']['numeric-id']}"
elif value_dict['type'] == 'time':
# Time type handling
return value_dict['value']['time']
except Exception as e:
logging.error(f"Error extracting object_id: {e}")
return None
return None
result_df['object_id'] = result_df['datavalue'].apply(extract_object_id)
# Extract unique Property IDs and Object IDs
property_ids = result_df['property_id'].unique().tolist()
# Separate time values and entity IDs
time_mask = result_df['object_id'].str.startswith('+', na=False) | result_df['object_id'].str.startswith('-', na=False)
entity_object_ids = [oid for oid in result_df[~time_mask]['object_id'].unique() if oid is not None]
# Get labels for properties and entity objects
property_labels = self.get_property_labels(property_ids)
entity_object_labels = self.get_entity_labels(entity_object_ids)
# Add labels to the DataFrame
result_df['property_label'] = result_df['property_id'].map(property_labels)
# For time values, use the time string as label
# For entity IDs, use the fetched labels
result_df.loc[time_mask, 'object_label'] = result_df.loc[time_mask, 'object_id']
result_df.loc[~time_mask, 'object_label'] = result_df.loc[~time_mask, 'object_id'].map(entity_object_labels)
# Drop datavalue column as it's no longer needed
result_df = result_df.drop('datavalue', axis=1)
return result_df
def get_property_labels(self, property_ids: List[str]) -> Dict[str, str]:
"""Fetch labels for Wikidata properties"""
endpoint_url = "https://query.wikidata.org/sparql"
query = f"""
SELECT ?id ?label WHERE {{
VALUES ?id {{ wd:{' wd:'.join(property_ids)} }}
?id rdfs:label ?label .
FILTER(LANG(?label) = "en" || LANG(?label) = "mul")
}}
"""
return self._execute_sparql_query(query)
def get_entity_labels(self, entity_ids: List[str]) -> Dict[str, str]:
"""Fetch labels for Wikidata entities"""
endpoint_url = "https://query.wikidata.org/sparql"
query = f"""
SELECT ?id ?label WHERE {{
VALUES ?id {{ wd:{' wd:'.join(entity_ids)} }}
?id rdfs:label ?label .
FILTER(LANG(?label) = "en" || LANG(?label) = "mul")
}}
"""
return self._execute_sparql_query(query)
def _execute_sparql_query(self, query: str) -> Dict[str, str]:
"""Execute SPARQL query and return results as a dictionary"""
endpoint_url = "https://query.wikidata.org/sparql"
headers = {
'User-Agent': 'Mozilla/5.0 (compatible; MyBot/1.0; mailto:[email protected])'
}
try:
r = requests.get(endpoint_url,
params={'format': 'json', 'query': query},
headers=headers)
r.raise_for_status()
results = r.json()
labels = {}
for result in results['results']['bindings']:
label = result['label']['value']
entity_id = result['id']['value'].split('/')[-1]
labels[entity_id] = label
return labels
except Exception as e:
logging.error(f"Error fetching labels: {e}")
return {}
if __name__ == "__main__":
qid = 'Q42'
# Get URLs from WikidataParser
from wikidata_parser import WikidataParser
parser = WikidataParser()
parser_result = parser.process_entity(qid)
url_references_df = parser_result['urls']
# Fetch HTML content with metadata
fetcher = HTMLFetcher(config_path='config.yaml')
result_df = fetcher.fetch_all_html(url_references_df, parser_result)
print(f"Successfully processed {len(result_df)} URLs")