Create a python web scraper that will walk through a single page, follow all links then…
- User
Create a python web scraper that will walk through a single page, follow all links then collect all of the key words and sentiment of the page.
Assistant
Sure, here's a basic Python web scraper that will follow all links on a given webpage and extract the key words and sentiment:
import requests from bs4 import BeautifulSoup def web_scraper(url): page = requests.get(url) soup = BeautifulSoup(page.content, 'html.parser') # Extract all links from the webpage links = [] for link in soup.find_all('a'): links.append(link.get('href')) # Follow all links and recursively scrape each page that is found for link in links: new_url = link if 'http' in new_url: new_page = requests.get(new_url) new_soup = BeautifulSoup(new_page.content, 'html.parser') # Extract key words and sentiment from the new page key_words = [] sentiment = 'negative' for tag in new_soup.find_all(attrs={'class': 'text-body__text'}): text = tag.text.strip() words = text.split() key_words.extend(words) sentiment = 'positive' if sentiment == 'negative else 'negative' # Print the new page's key words and sentiment print(f"Keywords: {key_words}") print(f"Sentiment: {sentiment}") else: print(f"Failed to follow link: {new_url}")
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.