-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscraper.py
More file actions
45 lines (35 loc) · 1.31 KB
/
scraper.py
File metadata and controls
45 lines (35 loc) · 1.31 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
import requests, bs4, html_wrapper
# // Scrape data from the provided url
def scrape_page(url: str, curr_resp_str: str) -> str:
# // Send a request to the url
r = requests.get(url, headers={
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36"
}, timeout=5)
# // Initialize bs4
soup = bs4.BeautifulSoup(r.text, 'html.parser')
# // Declare an empty response string
response_str: str = ""
# // Loop through the scrape results
for i in soup.find_all('li', class_='b_algo'):
# // Check if the result is none
if i is None:
continue
# // Verify the result has a div and paragraph
if i.p is None or i.div is None:
continue
# // Verify the result div has a header
header = i.div.h2
if header is None:
continue
# // Verify that the header has a url
if header.a is None:
continue
# // No duplicate searches!
if header.text not in curr_resp_str:
response_str += html_wrapper.search_result(
header.text,
i.p.text,
header.a['href']
)
# // Return the response string
return response_str