爬虫不会写?找ChatGPT不就完了,实战爬取某手办网~~~

这篇具有很好参考价值的文章主要介绍了爬虫不会写?找ChatGPT不就完了,实战爬取某手办网~~~。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

💗💗💗欢迎来到我的博客,你将找到有关如何使用技术解决问题的文章,也会找到某个技术的学习路线。无论你是何种职业,我都希望我的博客对你有所帮助。最后不要忘记订阅我的博客以获取最新文章,也欢迎在文章下方留下你的评论和反馈。我期待着与你分享知识、互相学习和建立一个积极的社区。谢谢你的光临,让我们一起踏上这个知识之旅!
爬虫不会写?找ChatGPT不就完了,实战爬取某手办网~~~,爬虫,爬虫,chatgpt,人工智能

🍋引言

今天来点有意思的,使用chatGPT如何爬取一个手办网
爬虫不会写?找ChatGPT不就完了,实战爬取某手办网~~~,爬虫,爬虫,chatgpt,人工智能

八、使用BeautifulSoup4解析HTML实战(二)
这篇文章的之前使用bs4进行爬取的文章,今天我们解放脑子,完全使用ChatGPT,接下来开始我的表演

🍋训练+代码

注意爬虫守则哈
爬虫不会写?找ChatGPT不就完了,实战爬取某手办网~~~,爬虫,爬虫,chatgpt,人工智能

接下来我们问它能不能,并且指明想要爬取的内容
爬虫不会写?找ChatGPT不就完了,实战爬取某手办网~~~,爬虫,爬虫,chatgpt,人工智能

接下来我们看看它生成的代码吧

import requests
from bs4 import BeautifulSoup

url = 'https://www.hpoi.net/hobby/all?order=add&category=100'
response = requests.get(url)

if response.status_code == 200:
    soup = BeautifulSoup(response.text, 'html.parser')

    # 在这里使用BeautifulSoup选择器获取所需信息
    # 请查看目标网页的HTML结构,使用合适的选择器来提取数据

    # 举例:获取所有产品名称
    names = soup.select('.product-name')

    # 举例:获取所有产品厂商
    manufacturers = soup.select('.product-manufacturer')

    # 举例:获取所有产品出荷日期
    ship_dates = soup.select('.product-ship-date')

    # 举例:获取所有产品新增日期
    add_dates = soup.select('.product-add-date')

    # 打印获取到的信息
    for name, manufacturer, ship_date, add_date in zip(names, manufacturers, ship_dates, add_dates):
        print(f'产品名称: {name.text.strip()}')
        print(f'产品厂商: {manufacturer.text.strip()}')
        print(f'出荷日期: {ship_date.text.strip()}')
        print(f'新增日期: {add_date.text.strip()}')
        print('---')

else:
    print(f'请求失败,状态码:{response.status_code}')

当然ChatGPT不会啥都会,比较网页的HTML它不知道,所以上述代码中的一些内容是例子,'.product-name’之类的需要我们自己去在网页找。当然迫不及待的小伙伴会直接运行,但是会发现啥也没有。我们需要再来问问Ai
爬虫不会写?找ChatGPT不就完了,实战爬取某手办网~~~,爬虫,爬虫,chatgpt,人工智能

import requests
from bs4 import BeautifulSoup

url = 'https://www.hpoi.net/hobby/all?order=add&category=100'

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}

response = requests.get(url, headers=headers)

if response.status_code == 200:
    soup = BeautifulSoup(response.text, 'html.parser')

    # 在这里使用BeautifulSoup选择器获取所需信息
    # 请查看目标网页的HTML结构,使用合适的选择器来提取数据

    # 举例:获取所有产品名称
    names = soup.select('.product-name')

    # 举例:获取所有产品厂商
    manufacturers = soup.select('.product-manufacturer')

    # 举例:获取所有产品出荷日期
    ship_dates = soup.select('.product-ship-date')

    # 举例:获取所有产品新增日期
    add_dates = soup.select('.product-add-date')

    # 打印获取到的信息
    for name, manufacturer, ship_date, add_date in zip(names, manufacturers, ship_dates, add_dates):
        print(f'产品名称: {name.text.strip()}')
        print(f'产品厂商: {manufacturer.text.strip()}')
        print(f'出荷日期: {ship_date.text.strip()}')
        print(f'新增日期: {add_date.text.strip()}')
        print('---')

else:
    print(f'请求失败,状态码:{response.status_code}')

不难发现上述代码加了一个请求头,目的就是让网页把你识别是一个正常用户,之后我们输出还是啥也没有,这时候就开始引导一些Ai了
爬虫不会写?找ChatGPT不就完了,实战爬取某手办网~~~,爬虫,爬虫,chatgpt,人工智能
爬虫不会写?找ChatGPT不就完了,实战爬取某手办网~~~,爬虫,爬虫,chatgpt,人工智能

从Ai的话可以理解,我们需要打印一下看看是HTML压根没有获取到还是什么问题。
我们试过之后发现HTML可以正常的获取,接下来我们就回到上面的问题,这个.product-name假的内容例子,我们需要替换成网页中的,该怎么办呢,这里教大家一招,我们将之前那个HTML保存下来,然后使用Ctrl+F找到一段结构,然后发给Ai让它帮你去做不就完了嘛,废话不多说,开干,我们玩的就是真实

爬虫不会写?找ChatGPT不就完了,实战爬取某手办网~~~,爬虫,爬虫,chatgpt,人工智能

import requests
from bs4 import BeautifulSoup

url = 'https://www.hpoi.net/hobby/all?order=add&category=100'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}

response = requests.get(url, headers=headers)

if response.status_code == 200:
    soup = BeautifulSoup(response.text, 'html.parser')

    # 将BeautifulSoup对象保存到文件
    with open('soup.html', 'w', encoding='utf-8') as file:
        file.write(soup.prettify())

    print('BeautifulSoup对象已保存到 soup.html 文件。')

else:
    print(f'请求失败,状态码:{response.status_code}')

保存后,我们来看看HTML
爬虫不会写?找ChatGPT不就完了,实战爬取某手办网~~~,爬虫,爬虫,chatgpt,人工智能
找到一个div,也就是其中一个要爬取的部分

<div class="hpoi-detail-grid-right">
          <div class="hpoi-detail-grid-title hpoi-text-ellipsis">
           <a href="hobby/92265" target="_blank" title="一番赏 五等分的新娘∽ ~五胞胎的庆生~ E奖 中野五月">
            一番赏 五等分的新娘∽ ~五胞胎的庆生~ E奖 中野五月
           </a>
          </div>
          <div class="hpoi-detail-grid-info">
           <span>
            <em>
             厂商:
            </em>
            BANDAI SPIRITS
           </span>
           <span>
            <em>
             出荷:
            </em>
            20244</span>
           <span>
            <em>
             新增:
            </em>
            1212</span>
          </div>
         </div>

然后问Ai
爬虫不会写?找ChatGPT不就完了,实战爬取某手办网~~~,爬虫,爬虫,chatgpt,人工智能
生成如下

import requests
from bs4 import BeautifulSoup

url = 'https://www.hpoi.net/hobby/all?order=add&category=100'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}

response = requests.get(url, headers=headers)

if response.status_code == 200:
    soup = BeautifulSoup(response.text, 'html.parser')

    # 获取所有产品信息
    product_elements = soup.select('.hpoi-detail-grid-right')

    # 打印获取到的信息
    for product in product_elements:
        name = product.select_one('.hpoi-detail-grid-title a').text.strip()
        manufacturer = product.select_one('em:contains("厂商:") + *').text.strip()
        ship_date = product.select_one('em:contains("出荷:") + *').text.strip()
        add_date = product.select_one('em:contains("新增:") + *').text.strip()

        print(f'产品名称: {name}')
        print(f'厂商: {manufacturer}')
        print(f'出荷日期: {ship_date}')
        print(f'新增日期: {add_date}')
        print('---')

else:
    print(f'请求失败,状态码:{response.status_code}')

但是会报错,我们接着问Ai就好,因为有的为空
爬虫不会写?找ChatGPT不就完了,实战爬取某手办网~~~,爬虫,爬虫,chatgpt,人工智能
大概需要重复三次,因为厂商、出荷、新增都有为空
最终得到的代码如下

import requests
from bs4 import BeautifulSoup

url = 'https://www.hpoi.net/hobby/all?order=add&category=100'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}

response = requests.get(url, headers=headers)

if response.status_code == 200:
    soup = BeautifulSoup(response.text, 'html.parser')

    # 获取所有产品信息
    product_elements = soup.select('.hpoi-detail-grid-right')

    # 打印获取到的信息
    for product in product_elements:
        name = product.select_one('.hpoi-detail-grid-title a').text.strip()
        manufacturer = product.select_one('em:contains("厂商:")')
        ship_date_element = product.select_one('em:contains("出荷:")')

        # 获取 "出荷:" 标签后的所有内容,然后提取文本
        if ship_date_element:
            ship_date_text = ship_date_element.find_next('span').get_text(strip=True)
        else:
            ship_date_text = 'N/A'

        add_date_element = product.select_one('em:contains("新增:")')

        # 获取 "新增:" 标签后的所有内容,然后提取文本
        if add_date_element:
            add_date_text = add_date_element.find_next('span').get_text(strip=True)
        else:
            add_date_text = 'N/A'

        # 检查厂商信息是否存在
        if manufacturer:
            manufacturer_text = manufacturer.find_next('span').text.strip()
        else:
            manufacturer_text = 'N/A'

        print(f'产品名称: {name}')
        print(f'厂商: {manufacturer_text}')
        print(f'出荷日期: {ship_date_text}')
        print(f'新增日期: {add_date_text}')
        print('---')

else:
    print(f'请求失败,状态码:{response.status_code}')

这回就可以打印出三十条数据了

🍋扩展

一般情况下,我们都不光打印一页,一般会好多页,大部分页与页直接会有一些相似处。
爬虫不会写?找ChatGPT不就完了,实战爬取某手办网~~~,爬虫,爬虫,chatgpt,人工智能

import requests
from bs4 import BeautifulSoup

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}

# 循环爬取前三页的数据
for page in range(1, 4):
    url = f'https://www.hpoi.net/hobby/all?order=add&r18=-1&workers=&view=3&category=100&page={page}'

    response = requests.get(url, headers=headers)

    if response.status_code == 200:
        soup = BeautifulSoup(response.text, 'html.parser')

        # 获取所有产品信息
        product_elements = soup.select('.hpoi-detail-grid-right')

        # 打印获取到的信息
        for product in product_elements:
            name = product.select_one('.hpoi-detail-grid-title a').text.strip()
            manufacturer = product.select_one('em:contains("厂商:")')
            ship_date_element = product.select_one('em:contains("出荷:")')
            
            # 获取 "出荷:" 标签后的所有内容,然后提取文本
            if ship_date_element:
                ship_date_text = ship_date_element.find_next('span').get_text(strip=True)
            else:
                ship_date_text = 'N/A'

            add_date_element = product.select_one('em:contains("新增:")')

            # 获取 "新增:" 标签后的所有内容,然后提取文本
            if add_date_element:
                add_date_text = add_date_element.find_next('span').get_text(strip=True)
            else:
                add_date_text = 'N/A'

            # 检查厂商信息是否存在
            if manufacturer:
                manufacturer_text = manufacturer.find_next('span').text.strip()
            else:
                manufacturer_text = 'N/A'

            print(f'产品名称: {name}')
            print(f'厂商: {manufacturer_text}')
            print(f'出荷日期: {ship_date_text}')
            print(f'新增日期: {add_date_text}')
            print('---')

        print(f'第 {page} 页数据已爬取完毕\n')

    else:
        print(f'请求失败,状态码:{response.status_code}')

之后将打印的内容保存到csv文件就可以了
爬虫不会写?找ChatGPT不就完了,实战爬取某手办网~~~,爬虫,爬虫,chatgpt,人工智能

import requests
from bs4 import BeautifulSoup
import csv

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}

# Open a CSV file for writing
with open('product_data.csv', 'w', newline='', encoding='utf-8') as csvfile:
    # Create a CSV writer object
    csv_writer = csv.writer(csvfile)

    # Write header row to CSV file
    csv_writer.writerow(['产品名称', '厂商', '出荷日期', '新增日期'])

    # Loop through each page
    for page in range(1, 4):
        url = f'https://www.hpoi.net/hobby/all?order=add&r18=-1&workers=&view=3&category=100&page={page}'

        response = requests.get(url, headers=headers)

        if response.status_code == 200:
            soup = BeautifulSoup(response.text, 'html.parser')

            # Get all product information
            product_elements = soup.select('.hpoi-detail-grid-right')

            # Loop through each product on the page
            for product in product_elements:
                name = product.select_one('.hpoi-detail-grid-title a').text.strip()
                manufacturer = product.select_one('em:contains("厂商:")')
                ship_date_element = product.select_one('em:contains("出荷:")')

                # Get "出荷:" element's text content
                if ship_date_element:
                    ship_date_text = ship_date_element.find_next('span').get_text(strip=True)
                else:
                    ship_date_text = 'N/A'

                add_date_element = product.select_one('em:contains("新增:")')

                # Get "新增:" element's text content
                if add_date_element:
                    add_date_text = add_date_element.find_next('span').get_text(strip=True)
                else:
                    add_date_text = 'N/A'

                # Check if manufacturer information exists
                if manufacturer:
                    manufacturer_text = manufacturer.find_next('span').text.strip()
                else:
                    manufacturer_text = 'N/A'

                # Print data to console
                print(f'产品名称: {name}')
                print(f'厂商: {manufacturer_text}')
                print(f'出荷日期: {ship_date_text}')
                print(f'新增日期: {add_date_text}')
                print('---')

                # Write data to CSV file
                csv_writer.writerow([name, manufacturer_text, ship_date_text, add_date_text])

            print(f'第 {page} 页数据已爬取完毕\n')

        else:
            print(f'请求失败,状态码:{response.status_code}')

print('数据已保存到 product_data.csv 文件。')

🍋解决保存后的csv文件,使用Excel打开总是乱码

爬虫不会写?找ChatGPT不就完了,实战爬取某手办网~~~,爬虫,爬虫,chatgpt,人工智能
将文件用记事本打开,使用ANSI编码即可,这样打开的文件就不会是乱码了
爬虫不会写?找ChatGPT不就完了,实战爬取某手办网~~~,爬虫,爬虫,chatgpt,人工智能

🍋总结

合理的利用Ai可以极大的提高我们的生产效率,但你也得会点,在自己有点基础的前提去使用会事半功倍。

爬虫不会写?找ChatGPT不就完了,实战爬取某手办网~~~,爬虫,爬虫,chatgpt,人工智能

挑战与创造都是很痛苦的,但是很充实。文章来源地址https://www.toymoban.com/news/detail-765345.html

到了这里,关于爬虫不会写?找ChatGPT不就完了,实战爬取某手办网~~~的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 爬虫之牛刀小试(十):爬取某宝手机商品的销量,价格和店铺

    首先淘宝需要登录,这一点如果用selenium如何解决,只能手动登录?如果不用selenium,用cookies登录也可。但是验证码又是一个问题,现在的验证码五花八门,难以处理。 我们回到正题,假设你已经登录上淘宝了,接着我们需要找到输入框和搜索按钮,输入“手机”,点击搜索

    2024年04月10日
    浏览(49)
  • python 爬取某站视频

    也是感觉好久都没有写博客了,主要是因为学业繁忙(其实是想多摆烂一会儿。。。) 距离暑假还有一个月,各科老师也开始布置相关的期末考试内容了。。。。。。 最近英语老师给我们留了一个期末作业(大学牲又要忙起来了),内容是拍摄一个短视频,既然是视频那素

    2024年02月04日
    浏览(35)
  • 爬取某音乐榜单歌曲

    一、打开网页https://music.163.com/,进入榜单(热歌榜) 二、右键检查、刷新网页,选择元素(点击歌曲名) 三、相关代码 四、爬取结果 GET请求(直链,hMusic高品质, mMusic中品质, lMusic低品质): GET请求(直链下载 含VIP):

    2024年02月07日
    浏览(27)
  • python批量爬取某站视频

    前言: 本项目是批量下载B站如下图示例的视频: (家里的小孩想看动画片,就下载到U盘上在电视上给他们放。。。) 在这个项目中,涉及到的模块有以下几个: 1.shutil: Python 标准库中的一个模块,用于文件操作,包括复制、移动、删除文件等。在这个项目中,主要用于创

    2024年02月20日
    浏览(30)
  • 六个步骤学会使用Python爬虫爬取数据(爬虫爬取微博实战)

    用python的爬虫爬取数据真的很简单,只要掌握这六步就好,也不复杂。以前还以为爬虫很难,结果一上手,从初学到把东西爬下来,一个小时都不到就解决了。 第一步:安装requests库和BeautifulSoup库 在程序中两个库的书写是这样的: 由于我使用的是pycharm进行的python编程。所以

    2024年02月08日
    浏览(38)
  • 使用python爬取某专科学校官方信息

    导入模块和代理设置:脚本开始时导入了必要的模块,如 csv, os, re, time, urllib.parse 中的 urljoin,bs4 中的 BeautifulSoup 和 selenium 中的 webdriver。同时为 HTTP 和 HTTPS 设置了代理。 CSV文件初始化:打开一个名为 ‘fzmjtc.csv’ 的CSV文件,用于存储提取的数据。文件以 UTF-8 编码写入,通

    2024年01月18日
    浏览(31)
  • python爬虫实战——小说爬取

    基于 requests 库和 lxml 库编写的爬虫,目标小说网站域名http://www.365kk.cc/,类似的小说网站殊途同归,均可采用本文方法爬取。 目标网站 :传送门 本文的目标书籍 :《我的师兄实在太稳健了》 “渡劫只有九成八的把握,和送死有什么区别?” 网络爬虫的工作实际上主要分为

    2024年02月06日
    浏览(31)
  • Python爬虫实战——爬取新闻数据(简单的深度爬虫)

            又到了爬新闻的环节(好像学爬虫都要去爬爬新闻,没办法谁让新闻一般都很好爬呢XD,拿来练练手),只作为技术分享,这一次要的数据是分在了两个界面,所以试一下深度爬虫,不过是很简单的。  网页url 1.先看看网站网址的规律  发现这部分就是每一天的新闻

    2024年02月11日
    浏览(31)
  • 爬虫项目实战——爬取B站视频

    目标:对B站视频详情页url进行视频的爬取。 注:由于B站的音频和视频的链接是分开的,所以在提取是需要分别提取,然后进行合成。 这里只管提取,合成的工作以后再说。 发送请求 对于视频详情页url地址发送请求 https://www.bilibili.com/video/BV11b4y1S7Jg 获取数据 获取响应体的文

    2024年02月07日
    浏览(32)
  • 【Python爬虫实战】爬虫封你ip就不会了?ip代理池安排上

    前言 在进行网络爬取时,使用代理是经常遇到的问题。由于某些网站的限制,我们可能会被封禁或者频繁访问时会遇到访问速度变慢等问题。因此,我们需要使用代理池来避免这些问题。本文将为大家介绍如何使用IP代理池进行爬虫,并带有代码和案例。 1. 什么是IP代理池

    2024年02月08日
    浏览(32)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包