-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathspider.py
More file actions
44 lines (33 loc) · 1.06 KB
/
spider.py
File metadata and controls
44 lines (33 loc) · 1.06 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
from urllib.parse import urljoin
import requests
from bs4 import BeautifulSoup
from urllib import *
visited_urls = set()
def spider_urls(url, keyword):
try:
response = requests.get(url)
except:
print(f"Request failed {url}")
return
if response.status_code == 200:
soup = BeautifulSoup(response.content, 'html.parser')
a_tag = soup.find_all('a')
urls = []
for tag in a_tag:
href = tag.get("href")
if href is not None and href != "":
urls.append(href)
# print(urls)
for urls2 in urls:
if urls2 not in visited_urls:
visited_urls.add(urls2)
url_join = urljoin(url, urls2)
if keyword in url_join:
print(url_join)
spider_urls(url_join, keyword)
else:
pass
# https://www.yahoo.com
url = input("Eneter the URL you want to scrap. ")
keyword = input("Enter the keyword to search for in the URL provided. ")
spider_urls(url, keyword)