AamirMalik commited on
Commit
1fb3118
·
verified ·
1 Parent(s): 19b7a1f

Update scraping_utils.py

Browse files
Files changed (1) hide show
  1. scraping_utils.py +16 -16
scraping_utils.py CHANGED
@@ -1,27 +1,27 @@
1
  import requests
2
- from bs4 import BeautifulSoup
3
 
4
- def search_web(query):
5
  """
6
- Perform a Google search for the query and return a list of results.
7
- Each result includes the title, link, and description.
8
  """
9
- url = f"https://www.google.com/search?q={query.replace(' ', '+')}+electronics+component"
10
- headers = {
11
- "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
12
- }
13
- response = requests.get(url, headers=headers, timeout=10)
14
 
 
15
  if response.status_code != 200:
16
  raise Exception(f"Failed to fetch search results: {response.status_code}")
17
 
18
- soup = BeautifulSoup(response.text, "html.parser")
19
  results = []
20
-
21
- for g in soup.find_all("div", class_="tF2Cxc"):
22
- title = g.find("h3").text if g.find("h3") else "No title available"
23
- link = g.find("a")["href"] if g.find("a") else "No link available"
24
- description = g.find("span", class_="aCOpRe").text if g.find("span", class_="aCOpRe") else "No description available"
25
- results.append({"title": title, "link": link, "description": description})
26
 
27
  return results
 
1
  import requests
 
2
 
3
+ def search_bing(query):
4
  """
5
+ Perform a search using Bing Search API and return a list of results.
6
+ Each result includes the title, link, and snippet (description).
7
  """
8
+ # Replace 'YOUR_BING_API_KEY' with your Bing Search API key
9
+ api_key = "YOUR_BING_API_KEY"
10
+ endpoint = "https://api.bing.microsoft.com/v7.0/search"
11
+ headers = {"Ocp-Apim-Subscription-Key": api_key}
12
+ params = {"q": query, "count": 5, "textFormat": "Raw"}
13
 
14
+ response = requests.get(endpoint, headers=headers, params=params, timeout=10)
15
  if response.status_code != 200:
16
  raise Exception(f"Failed to fetch search results: {response.status_code}")
17
 
18
+ data = response.json()
19
  results = []
20
+ for result in data.get("webPages", {}).get("value", []):
21
+ results.append({
22
+ "title": result["name"],
23
+ "link": result["url"],
24
+ "snippet": result["snippet"]
25
+ })
26
 
27
  return results