You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何在网站中执行Python代码?适配requests/BeautifulSoup爬虫代码需求

解决方案:将eBay爬虫集成到网站的后端API方案

你遇到的问题很典型——这段Python爬虫代码是本地运行的脚本,直接在网站前端没法执行(前端是JS环境),而且requestsBeautifulSoup都是后端Python依赖,必须把爬虫逻辑放到Web后端服务里,通过API接口给前端返回数据。下面是用轻量框架Flask实现的可行方案,步骤清晰,能直接复用:


步骤1:准备后端环境

首先确保你安装了必要的依赖:

pip install flask requests beautifulsoup4

步骤2:改造爬虫代码为API接口

把原来的print输出改成返回结构化数据(比如JSON),并添加异常处理和反爬措施(eBay会拦截无标识的请求):

from flask import Flask, jsonify, request
import requests
from bs4 import BeautifulSoup

app = Flask(__name__)

# 配置请求头,模拟浏览器访问,避免被eBay反爬
HEADERS = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36'
}

def get_item_price(item_url):
    try:
        src = requests.get(item_url, headers=HEADERS)
        src.raise_for_status()  # 抛出HTTP请求异常
        soup = BeautifulSoup(src.text, "html.parser")
        item_price = soup.find('span', {'class': 'notranslate'})
        if item_price:
            return item_price.string.strip()
        return "Price unavailable"
    except Exception as e:
        return f"Error fetching price: {str(e)}"

def trade_spider(max_pages=1, search_term='Ipad'):
    results = []
    page = 1
    while page <= max_pages:
        url = f'https://www.ebay.com/sch/i.html?_from=R40&_nkw={search_term}&_sacat=0&_pgn={page}'
        try:
            src = requests.get(url, headers=HEADERS)
            src.raise_for_status()
            soup = BeautifulSoup(src.text, "html.parser")
            for link in soup.findAll('a', {'class': 's-item__link'}):
                href = link.get('href')
                title = link.string.strip() if link.string else "Title unavailable"
                price = get_item_price(href)
                results.append({
                    'title': title,
                    'url': href,
                    'price': price
                })
            page += 1
        except Exception as e:
            results.append({'error': f"Error fetching page {page}: {str(e)}"})
            break
    return results

# 定义API路由,前端可以通过这个接口获取数据
@app.route('/api/ebay-search')
def ebay_search():
    # 通过URL参数接收搜索关键词和页数,比如/api/ebay-search?term=Ipad&pages=1
    search_term = request.args.get('term', 'Ipad')
    max_pages = int(request.args.get('pages', 1))
    data = trade_spider(max_pages, search_term)
    return jsonify(data)

if __name__ == '__main__':
    app.run(debug=True)  # 开发环境用,生产环境要换成WSGI服务器比如Gunicorn

步骤3:前端调用API展示数据

你可以用简单的HTML+JavaScript来请求后端API并展示结果,示例如下:

<!DOCTYPE html>
<html>
<head>
    <title>eBay Search Results</title>
</head>
<body>
    <h1>eBay Ipad Listings</h1>
    <div id="results"></div>

    <script>
        // 调用后端API
        fetch('/api/ebay-search?term=Ipad&pages=1')
            .then(response => response.json())
            .then(data => {
                const resultsDiv = document.getElementById('results');
                data.forEach(item => {
                    if (item.error) {
                        resultsDiv.innerHTML += `<p style="color:red">${item.error}</p>`;
                    } else {
                        resultsDiv.innerHTML += `
                            <div style="border:1px solid #ccc; padding:10px; margin:10px 0;">
                                <h3><a href="${item.url}" target="_blank">${item.title}</a></h3>
                                <p>Price: ${item.price}</p>
                            </div>
                        `;
                    }
                });
            })
            .catch(error => {
                document.getElementById('results').innerHTML = `<p style="color:red">Failed to load data: ${error}</p>`;
            });
    </script>
</body>
</html>

关键注意事项

  • 反爬规避:一定要设置User-Agent,否则eBay会直接拒绝你的请求。如果请求频繁,还可以添加代理IP或者请求间隔(比如time.sleep(1))。
  • 遵守网站规则:查看eBay的robots.txt,确保你的爬虫行为符合规定,避免被封禁IP。
  • 生产环境部署:开发环境用app.run()没问题,生产环境要使用WSGI服务器(比如Gunicorn),并配合Nginx反向代理。
  • 异常处理:代码里已经添加了基础的异常捕获,你可以根据需要扩展(比如处理网络超时、页面结构变化等)。

内容的提问来源于stack exchange,提问作者Ghassan Shahzad

火山引擎 最新活动