-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrawler.py
78 lines (55 loc) · 2.47 KB
/
crawler.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
import requests
from bs4 import BeautifulSoup
import csv
import json
from urllib.parse import urlencode
base_url = "https://google.com/search"
def generate_url(params):
url = base_url + '?q=' + params["primary_category"].replace(' ', '+')+"+"+params["secondary_category"].replace(' ', '+')+"+"+params["geography"].replace(' ', '+')+"+"+params["date_range"]
print (url)
return url
def crawl_websites(params):
primary_category = params.get("Primary Category", "")
secondary_category = params.get("Secondary Category", "")
geography = params.get("Geography", "")
date_range = params.get("Date Range", "")
url = generate_url({
"primary_category": primary_category,
"secondary_category": secondary_category,
"geography": geography,
"date_range": date_range,
})
response = requests.get(url)
if response.status_code == 200:
soup = BeautifulSoup(response.text, "html.parser")
links = soup.find_all("a")
filtered_links = [link.get("href") for link in links if "https" in link.get("href", "") and "google" not in link.get("href", "")]
with open('output.csv', 'w', newline='') as csvfile:
fieldnames = ['Link']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for link in filtered_links:
writer.writerow({'Link': link})
print("Links stored in output.csv")
else:
print(f"Failed to retrieve the page. Status code: {response.status_code}")
import csv
def create_substring_csv(input_filename, output_filename):
with open(input_filename, 'r', newline='') as csvfile:
reader = csv.DictReader(csvfile)
links = [row['Link'] for row in reader]
with open(output_filename, 'w', newline='') as csvfile:
fieldnames = ['SubStringed_Link']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for link in links:
start_index = link.find("https")
end_index = link.find("&") if "&" in link else None
substringed_link = link[start_index:end_index]
writer.writerow({'SubStringed_Link': substringed_link})
print(f"Substringed links stored in {output_filename}")
if __name__ == "__main__":
with open('parameters.json', 'r') as json_file:
parameters = json.load(json_file)
crawl_websites(parameters)
create_substring_csv('output.csv', 'output_filtered.csv')