• 【python爬虫】闲鱼爬虫,可以爬取商品


    目录

    前言

    一、介绍

    二、爬虫流程

    1. 确定关键词并构造URL

    2. 发送网络请求

    3. 解析HTML并提取数据

    4. 保存数据

    三、使用代理IP

    四、完整代码

    五、总结


    前言

    闲鱼是一个很受欢迎的二手交易平台,但是由于没有开放API,我们需要使用爬虫来获取数据。本文将介绍如何使用Python爬虫爬取闲鱼上的商品信息,包括构造URL、发送网络请求、解析HTML并提取数据以及使用代理IP来进行爬取。如果您需要抓取闲鱼的其他数据,本文也提供了一些参考。

    一、介绍

    随着电子商务的兴起,二手交易平台也变得越来越受欢迎。作为淘宝旗下的二手交易平台,闲鱼的日活跃用户已经超过了1亿。因此,对于一些商家和买家来说,闲鱼是一个极具吸引力的平台。

    对于我们开发者来说,有时候我们需要从闲鱼上抓取一些数据,比如价格走势,热门商品,关键词排名等等。但是,闲鱼并没有开放API,这就需要我们使用爬虫来获取数据。

    本文将详细介绍如何使用Python爬虫爬取闲鱼上的商品信息。我们将主要使用requests库和BeautifulSoup库来完成这个任务。此外,为了避免被闲鱼封禁IP地址,我们还将使用代理IP来进行爬取。

    二、爬虫流程

    要完成我们的闲鱼爬虫,我们需要掌握以下几个步骤:

    1. 确定关键词并构造URL

    在爬取闲鱼数据之前,首先我们需要确定要搜索的关键词。这个关键词可以是任何你想要的内容,比如“二手手机”、“二手电脑”等等。

    根据我们选择的关键词,我们需要构造一个URL,即闲鱼商品搜索的URL。URL的构造方法如下:

    url = "https://2.taobao.com/search/index.htm?q={}&search_type=item&app=shopsearch".format(keyword)

    其中,keyword为我们选择的关键词。

    2. 发送网络请求

    我们使用requests库来发送网络请求:

    1. headers = {
    2.    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Safari/537.36'}
    3. response = requests.get(url, headers=headers)

    在发送请求之前,我们需要设置请求头。这个请求头包含了我们浏览器的信息,这样可以避免被服务器轻易识别为爬虫。

    3. 解析HTML并提取数据

    我们使用BeautifulSoup库来解析HTML并提取数据:

    1. soup = BeautifulSoup(response.text, 'html.parser')
    2. goods_list = soup.find_all('div', {'class': 'J_MouserOnverReq'})

    解析完HTML之后,我们需要找出包含商品信息的标签。我们可以通过查看闲鱼网页的源代码,找到所有商品信息都被包含在一个class为“J_MouserOnverReq”的div中。

    4. 保存数据

    最后一步,我们需要将爬取到的数据保存下来。这里我们使用csv库将数据保存到csv文件中。

    1. with open('goods_info.csv', 'w', newline='') as f:
    2.     writer = csv.writer(f)
    3.     writer.writerow(['商品名称', '商品价格', '商品链接'])
    4.     for goods in goods_list:
    5.         title = goods.find('p', {'class': 'item-title'}).text.strip()
    6.         price = goods.find('p', {'class': 'price'}).text.strip()
    7.         link = goods.find('a', {'class': 'item-link'}).get('href')
    8.         writer.writerow([title, price, link])

    通过使用以上四个步骤,我们可以完成闲鱼商品信息的爬虫。

    三、使用代理IP

    由于频繁的发送网络请求会使服务器怀疑我们是爬虫,并且封禁我们的IP地址,所以我们需要使用代理IP来隐藏我们的真实IP地址。

    我们可以从代理IP网站上获取代理IP。这里我们使用站大爷的API,可以通过以下的代码来获取代理IP:

    1. def get_proxies():
    2.     response = requests.get("http://ip.zdaye.com/dayProxy.html")
    3.     soup = BeautifulSoup(response.text, 'html.parser')
    4.     trs = soup.find_all('tr')
    5.     proxies = []
    6.     for tr in trs[1:]:
    7.         tds = tr.find_all('td')
    8.         ip = tds[0].text.strip()
    9.         port = tds[1].text.strip()
    10.         protocol = tds[3].text.strip().lower()
    11.         proxies.append("{}://{}:{}".format(protocol, ip, port))
    12.     return proxies

    该函数会返回一个代理IP池。

    我们可以在发送网络请求的时候使用代理IP,代码如下:

    1. proxies = {
    2.     "http": random.choice(get_proxies()),
    3.     "https": random.choice(get_proxies())
    4. }
    5. response = requests.get(url, headers=headers, proxies=proxies)

    在构造requests对象的时候,我们传入proxies参数,代表我们使用一个代理IP来发送网络请求。

    四、完整代码

    1. import csv
    2. import random
    3. import requests
    4. from bs4 import BeautifulSoup
    5. def get_proxies():
    6.     """
    7.     获取代理IP
    8.     """
    9.     response = requests.get("http://ip.zdaye.com/dayProxy.html")
    10.     soup = BeautifulSoup(response.text, 'html.parser')
    11.     trs = soup.find_all('tr')
    12.     proxies = []
    13.     for tr in trs[1:]:
    14.         tds = tr.find_all('td')
    15.         ip = tds[0].text.strip()
    16.         port = tds[1].text.strip()
    17.         protocol = tds[3].text.strip().lower()
    18.         proxies.append("{}://{}:{}".format(protocol, ip, port))
    19.     return proxies
    20. def get_goods_info(keyword):
    21.     """
    22.     爬取商品信息
    23.     """
    24.     url = "https://2.taobao.com/search/index.htm?q={}&search_type=item&app=shopsearch".format(keyword)
    25.     headers = {
    26.         'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
    27.                       'Chrome/58.0.3029.96 Safari/537.36'}
    28.     proxies = {
    29.         "http": random.choice(get_proxies()),
    30.         "https": random.choice(get_proxies())
    31.     }
    32.     response = requests.get(url, headers=headers, proxies=proxies)
    33.     soup = BeautifulSoup(response.text, 'html.parser')
    34.     goods_list = soup.find_all('div', {'class': 'J_MouserOnverReq'})
    35.     with open('goods_info.csv', 'w', newline='') as f:
    36.         writer = csv.writer(f)
    37.         writer.writerow(['商品名称', '商品价格', '商品链接'])
    38.         for goods in goods_list:
    39.             title = goods.find('p', {'class': 'item-title'}).text.strip()
    40.             price = goods.find('p', {'class': 'price'}).text.strip()
    41.             link = goods.find('a', {'class': 'item-link'}).get('href')
    42.             writer.writerow([title, price, link])
    43. if __name__ == '__main__':
    44.     get_goods_info('二手手机')

    五、总结

    本文介绍了如何使用Python爬虫爬取闲鱼上的商品信息,并且使用代理IP防止被封禁IP地址。如果您还需要爬取其他数据,比如评论、店铺信息等等,您可以根据本文提到的方法进行尝试。

  • 相关阅读:
    Azure 机器学习 - 搜索中的检索增强 (RAG)
    算力服务网络:一场多元融合的系统革命
    高性能MySQL(第4版) 第一章 MySQL架构 读书笔记
    2023最新盲盒交友脱单系统源码
    ClickHouse 挺快,esProc SPL 更快
    编译,连接 -- 笔记 -2
    SpringBoot autoconfigure
    C++:AVL树
    Lazy Initialized Beans
    Ajax技术
  • 原文地址:https://blog.csdn.net/wq10_12/article/details/133637616