-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
247 lines (200 loc) · 8.41 KB
/
test.py
File metadata and controls
247 lines (200 loc) · 8.41 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
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
import requests
import urllib.parse
import sys
import time
import random
from concurrent.futures import ThreadPoolExecutor
from bs4 import BeautifulSoup
def search_duckduckgo(query, num_results=10):
"""
Perform a search on DuckDuckGo and return the results.
Args:
query (str): The search query
num_results (int): Number of results to return
Returns:
list: A list of dictionaries containing search results
"""
# URL encode the query
encoded_query = urllib.parse.quote_plus(query)
# Construct the search URL
url = f"https://html.duckduckgo.com/html/?q={encoded_query}"
# Set headers to mimic a browser
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.5',
'Referer': 'https://duckduckgo.com/'
}
try:
# Send the request
response = requests.get(url, headers=headers)
response.raise_for_status()
# Parse the HTML
soup = BeautifulSoup(response.text, 'html.parser')
# Extract search results
results = []
for result in soup.select('.result')[:num_results]:
title_element = result.select_one('.result__title a')
snippet_element = result.select_one('.result__snippet')
if title_element and snippet_element:
title = title_element.text.strip()
snippet = snippet_element.text.strip()
url = title_element.get('href')
if url and url.startswith('/'):
url_params = urllib.parse.parse_qs(url.split('?')[1])
if 'uddg' in url_params:
url = url_params['uddg'][0]
results.append({
'title': title,
'snippet': snippet,
'url': url
})
return results
except Exception as e:
print(f"Error during search: {e}")
return []
def clean_url(url):
"""Clean and normalize URLs from search results."""
# Handle ad URLs that start with duckduckgo.com/y.js
if url.startswith('https://duckduckgo.com/y.js?'):
try:
# Make a HEAD request to follow redirects
response = requests.head(url, allow_redirects=True, timeout=5)
return response.url
except:
return url
return url
def scrape_website(url):
"""Scrape content from a website URL."""
url = clean_url(url)
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.5'
}
try:
response = requests.get(url, headers=headers, timeout=10)
response.raise_for_status()
# Use the content's detected encoding
soup = BeautifulSoup(response.content, 'html.parser', from_encoding=response.encoding)
# Get page title
title = soup.title.text.strip() if soup.title else "No title"
# Remove script and style tags
for script in soup(["script", "style"]):
script.extract()
# Extract main content based on common content containers
main_content = None
for selector in ['main', 'article', '.content', '#content', '.main', '.article', '.post']:
main_content = soup.select_one(selector)
if main_content:
break
# If no main content container found, use the body
if not main_content:
main_content = soup.body
paragraphs = []
if main_content:
# Get all text paragraphs
for p in main_content.find_all(['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'li']):
text = p.get_text().strip()
if text: # Any non-empty paragraph
paragraphs.append(text)
# If we didn't get content, try a different approach
if not paragraphs:
text = soup.get_text()
# Remove extra whitespace
import re
text = re.sub(r'\s+', ' ', text).strip()
# Split into sentences
sentences = re.split(r'(?<=[.!?])\s+', text)
paragraphs = [s for s in sentences if len(s) > 10]
return {
"url": url,
"title": title,
"content": paragraphs,
"success": True
}
except Exception as e:
print(f"Error scraping {url}: {e}")
return {
"url": url,
"title": "Failed to scrape",
"content": [f"Error: {str(e)}"],
"success": False
}
def search_and_scrape(query, num_results=8, max_paragraphs=50):
"""
Search for a query on DuckDuckGo, scrape the websites from the results,
and return all the data.
Args:
query (str): The search query
num_results (int): Number of search results to process
max_paragraphs (int): Maximum number of paragraphs to return per site
Returns:
dict: Dictionary containing search results and scraped content
"""
print(f"Searching for: {query}")
search_results = search_duckduckgo(query, num_results)
if not search_results:
return {
"query": query,
"search_results": [],
"scraped_data": [],
"error": "No search results found"
}
print(f"Found {len(search_results)} results. Scraping websites...")
# Scrape websites using multithreading
scraped_data = []
with ThreadPoolExecutor(max_workers=5) as executor:
future_to_url = {executor.submit(scrape_website, result['url']): result for result in search_results}
for future in future_to_url:
try:
site_data = future.result()
# Limit number of paragraphs to avoid overwhelming amounts of data
if len(site_data['content']) > max_paragraphs:
site_data['content'] = site_data['content'][:max_paragraphs]
scraped_data.append(site_data)
print(f"Scraped: {site_data['url']}")
# Sleep a bit to avoid overloading websites
time.sleep(random.uniform(0.5, 1))
except Exception as e:
print(f"Error processing site: {e}")
return {
"query": query,
"search_results": search_results,
"scraped_data": scraped_data
}
def print_results(results):
"""Print the search and scraping results in a readable format."""
print(f"\n{'='*80}")
print(f"RESULTS FOR: {results['query']}")
print(f"{'='*80}\n")
if "error" in results:
print(f"ERROR: {results['error']}")
return
print(f"Found {len(results['search_results'])} search results and scraped {len(results['scraped_data'])} websites.\n")
for i, site in enumerate(results['scraped_data'], 1):
print(f"\n{'-'*80}")
print(f"SITE {i}: {site['title']}")
print(f"URL: {site['url']}")
print(f"{'-'*80}\n")
if not site['success']:
print(f"Failed to scrape this site: {site['content'][0]}")
continue
print(f"CONTENT (showing {len(site['content'])} paragraphs):")
for j, paragraph in enumerate(site['content'], 1):
# Limit paragraph length for display
if len(paragraph) > 300:
paragraph = paragraph[:300] + "..."
print(f"{j}. {paragraph}\n")
if __name__ == "__main__":
if len(sys.argv) > 1:
query = " ".join(sys.argv[1:])
else:
query = input("Enter your search query: ")
results = search_and_scrape(query)
print_results(results)
# You can also save the results to a file
import json
with open(f"{query.replace(' ', '_')}_results.json", "w", encoding="utf-8") as f:
json.dump(results, f, ensure_ascii=False, indent=2)
print(f"\nResults saved to {query.replace(' ', '_')}_results.json")