Web Scraping with ChatGPT

Hướng dẫn, làm thế nào để, cạo, Aug-02-20245 phút đọc

Web scraping is a powerful tool for developers, data scientists, digital marketers and many other people who wish to extract valuable data from websites. If you're looking to elevate your web scraping journey, harnessing the capabilities of ChatGPT can help you a lot. This blog will guide you through using ChatGPT to create robust, efficient, and reliable web scraping scripts.

Introduction to ChatGPT

ChatGPT, powered by OpenAI, is a state-of-the-art language model designed to understand and generate human-like text. It leverages natural language processing (NLP) to assist in a variety of tasks, ranging from content creation to coding assistance. With its ability to comprehend context and provide intelligent suggestions, ChatGPT has become a valuable asset for developers and data scientists.

What is ChatGpt?

ChatGPT stands for "Chat Generative Pre-trained Transformer." It's a type of artificial intelligence that can generate text based on the input it receives. While it's known for conversational abilities, its applications extend far beyond simple dialogue.

Usages of ChatGpt in web scraping

  • Code Assistance: Help with writing and debugging web scraping scripts.
  • Library Recommendations: Suggest tools like Beautiful Soup, Scrapy, Selenium...
  • Best Practices: Guidance on ethical and efficient scraping techniques.

Limitations of ChatGPT in Web Scraping

  • Bypassing Security Measures: ChatGPT cannot help circumvent security measures like IP blocking or other anti-scraping technologies beyond ethical limits.
  • Real-time Data Collection: ChatGPT cannot interact with websites in real-time to collect data.
  • Custom Tool Development: ChatGPT cannot develop custom software tools or frameworks for web scraping.

Example on how to use ChatGpt for web scraping

While chat cannot directly scrape a website, it can certainly inspire us with ideas on how to approach the web scraping process. Additionally, it can provide scripts that we can utilize in our web scraping projects.

Let’s explore a simple example. Imagine we want to scrape a blog post website and extract each blog post, and store it in a csv file. The information we want to save is the blog tittle, description and blog url.

Step 1 - Compose ChatGpt Prompt:

First, we need to create a prompt for ChatGPT that clearly states what we need. In this example, we will use the website (https://www.scrapethissite.com/pages) to extract the title, description and URL of each blog.To instruct ChatGpt correctly, we need to provide the selectors of the first blog. To do that right-click on the element and then click inspect

After that we want to grab the Xpath selector by again right-clicking on the element, then Copy and then Copy XPath

You should also apply the same to the description and URL sections of the blog.

Below is the final version of the prompt:

Develop a Python script using 'Requests' and 'BeautifulSoup' library that scrapes blog posts from this website: "https://www.scrapethissite.com/pages/"
The information that needs to be extracted is: 
- Blog Title - XPath: "//*[@id="pages"]/section/div/div/div/div[1]/h3/a" 
- Blog Description - XPath: "//*[@id="pages"]/section/div/div/div/div[1]/p"- Blog Url 
- XPath: "//*[@id="pages"]/section/div/div/div/div[1]/h3/a"

At the end, the script should print the results and store them in a csv file.

Step 2 - Setup Python Script:

Here is the script that ChatGpt outputted:

import requests
from bs4 import BeautifulSoup
import csv
# URL of the website to scrape
url = 'https://www.scrapethissite.com/pages/'
# Send a GET request to the URL
response = requests.get(url)
# Parse the page content using BeautifulSoup
soup = BeautifulSoup(response.text, 'html.parser')
# List to store the scraped data
blogs = []
# Locate the sections containing blog information
# Using the provided XPath details converted to BeautifulSoup format
blog_sections = soup.select('#pages section div div div div')
for section in blog_sections:
   # Extract the blog title
   title_tag = section.select_one('h3 a')
   title = title_tag.text.strip() if title_tag else 'No title'
   # Extract the blog description
   description_tag = section.select_one('p')
   description = description_tag.text.strip() if description_tag else 'No description'
   # Extract the blog URL
   url_tag = section.select_one('h3 a')
   blog_url = url_tag['href'] if url_tag else 'No URL'
   # Append the data to the blogs list
   blogs.append({'Title': title, 'Description': description, 'URL': blog_url})
# Print the extracted information
for blog in blogs:
   print(f"Title: {blog['Title']}\nDescription: {blog['Description']}\nURL: {blog['URL']}\n")
# Save the data to a CSV file
csv_file = 'blogs.csv'
with open(csv_file, mode='w', newline='') as file:
   writer = csv.DictWriter(file, fieldnames=['Title', 'Description', 'URL'])
   writer.writeheader()
   writer.writerows(blogs)
if __name__ == '__main__':
   print(f"Data saved to {csv_file}")

Before running this script, ensure you have installed the 'requests' and 'bs4' libraries.

pip install requests bs4

Here’s a brief overview of what this script does:

  • Import Libraries: Imports requests, BeautifulSoup, and csv for handling HTTP requests, parsing HTML, and managing CSV file operations.
  • Fetch Web Page Content: Uses requests to send a GET request to the specified URL and retrieve the HTML content of the page.
  • Parse HTML Content: Parses the retrieved HTML using BeautifulSoup to facilitate data extraction.
  • Extract Blog Information:
    • Blog Title: Extracts the title of each blog post.
    • Blog Description: Extracts the description of each blog post.
    • Blog URL: Extracts the URL of each blog post.
  • Store Data: Stores the extracted data in a list of dictionaries.
  • Print Extracted Data: Prints the title, description, and URL of each blog post.
  • Save Data to CSV: Saves the extracted data to a CSV file named blogs.csv.

Step 3 - Test The Script:

Once you have installed the necessary libraries, create a Python file with your preferred name. Then, paste the script into the file and save it.

Once you execute the script, it will print data for each blog and generate a CSV file named "blogs.csv." Here’s what it looks like:

Kết thúc

ChatGPT is a valuable tool for developers, data scientists, and web scraping enthusiasts. By leveraging its capabilities, you can enhance your web scraping scripts, improve accuracy, and reduce development time. Whether you're extracting data for market analysis, social media monitoring, or academic research, ChatGPT can help you achieve your goals more efficiently.