-
Notifications
You must be signed in to change notification settings - Fork 189
Expand file tree
/
Copy pathbing_search_scraper.py
More file actions
26 lines (20 loc) · 962 Bytes
/
bing_search_scraper.py
File metadata and controls
26 lines (20 loc) · 962 Bytes
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
import requests
from bs4 import BeautifulSoup
def scrape_bing(query):
url = f"https://www.bing.com/search?q={query}"
headers = {"User-Agent": "Mozilla/5.0"}
response = requests.get(url, headers=headers)
if response.status_code != 200:
print("Failed to retrieve search results") # handling errors
return []
soup = BeautifulSoup(response.text, "html.parser") # parsing the info
results = [] # empty list to store data
for item in soup.find_all("li", class_="b_algo"):
title = item.find("h2") # extracting the result title
link = title.find("a")["href"] if title else "" # extracting the link
results.append((title.text if title else "No title", link))
# if title is not found, just returns the link with "No title" placeholder
return results
search_results = scrape_bing("apify") # replace your seach query
for title, link in search_results:
print(f"{title}: {link}")