希望提供帮助?以下是您的选择:","Crunchbase","关于我们","感谢大家的大力支持!","快速链接","联属会员计划","高级","ProxyScrape 高级试用","代理类型","代理国家","代理用例","重要","Cookie 政策","免责声明","隐私政策","条款和条件","社交媒体","在 Facebook 上","LinkedIn","推特","Quora","电报","不和谐音","\n © Copyright 2025 -Thib BV| Brugstraat 18 | 2812 Mechelen | Belgium | VAT BE 0749 716 760\n"]}
对于开发人员、数据科学家、数字营销人员和其他许多希望从网站中提取有价值数据的人来说,网络搜刮是一个强大的工具。如果你想提升你的网络抓取之旅,利用 ChatGPT 的功能可以帮到你很多。本篇博客将指导你使用 ChatGPT 创建强大、高效、可靠的网页抓取脚本。
由 OpenAI 提供支持的 ChatGPT 是一种先进的语言模型,旨在理解和生成类人文本。它利用自然语言处理(NLP)技术协助完成从内容创建到编码协助等各种任务。凭借其理解上下文和提供智能建议的能力,ChatGPT 已成为开发人员和数据科学家的宝贵资产。
ChatGPT 是 "聊天生成预训练转换器 "的缩写。它是一种人工智能,可以根据接收到的输入生成文本。虽然它以对话能力著称,但其应用范围远远超出了简单的对话。
虽然聊天不能直接搜刮网站,但它肯定能启发我们如何处理网络搜刮过程。此外,它还可以提供脚本,供我们在网络刮擦项目中使用。
让我们来看一个简单的例子。想象一下,我们想抓取一个博文网站,提取每篇博文,并将其存储到一个 csv 文件中。我们要保存的信息是博客标题、描述和博客网址。
首先,我们需要为 ChatGPT 创建一个提示,明确说明我们的需求。在本例中,我们将使用网站 (https://www.scrapethissite.com/pages) 来提取每个博客的标题、描述和 URL。为了正确指导 ChatGpt,我们需要提供第一个博客的选择器。为此,右键单击元素,然后单击检查
之后,我们要再次右键单击元素,获取 Xpath 选择器,然后 复制 然后 复制 XPath
博客的描述和 URL 部分也应如此。
以下是提示语的最终版本:
使用"Requests " 和 "BeautifulSoup "库开发一个 Python脚本 ,从该网站抓取 博客 文章:"https://www.scrapethissite.com/pages/"
需要提取 的 信息是
- 博客 标题 - XPath:"//*[@id="pages"]/section/div/div/div[1] /h3/a"。
- 博客 描述 - XPath:"//*[@id="页面"]/section/div/div/div[1]/p"-博客 网址
- XPath:"//*[@id="页面"]/section/div/div/div[1]/h3/a"
最后,脚本 应 打印结果并 将其存储到 csv 文件中。
下面是 ChatGpt 输出的脚本:
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}")
运行此脚本前,请确保已安装 "requests "和 "bs4 "库。
pipinstall requestsbs4
下面简要介绍一下这个脚本的作用:
安装完必要的库后,创建一个 Python 文件,文件名可自行选择。然后,将脚本粘贴到文件中并保存。
执行脚本后,它会打印每个博客的数据,并生成一个名为 "blogs.csv "的 CSV 文件。下面就是它的样子:
ChatGPT 是开发人员、数据科学家和网络搜索爱好者的重要工具。利用它的功能,您可以增强网络抓取脚本、提高准确性并缩短开发时间。无论您是提取数据用于市场分析、社交媒体监测还是学术研究,ChatGPT 都能帮助您更高效地实现目标。