You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多
文档控制台
免费开始使用

如何将Python脚本与Android应用连接?实现爬虫数据在Android端展示

Hey there! Let's break down how you can replace your PHP API setup with Python to handle web scraping and serve data to your Android app via JSON—just like you're used to doing with PHP. Here's a step-by-step guide tailored to your needs:

1. Build a Python Web API (Your PHP Replacement)

First, you'll need a lightweight web framework to create API endpoints. Flask is perfect for this (simple, easy to set up, and works great for small to medium APIs). FastAPI is another solid option if you want async support and automatic docs, but let's start with Flask for simplicity.

Step 1: Install Dependencies

Open your terminal and install the required packages:

pip install flask requests beautifulsoup4 flask-cors
  • flask: Creates the API server
  • requests: Handles HTTP requests for scraping
  • beautifulsoup4: Parses HTML/XML to extract data
  • flask-cors: Fixes cross-origin issues (critical for testing with Android)

Step 2: Write the API + Scraping Logic

Create a file (e.g., app.py) with this code:

from flask import Flask, jsonify
from flask_cors import CORS
import requests
from bs4 import BeautifulSoup

app = Flask(__name__)
CORS(app)  # Allow cross-origin requests from Android

# Define your API endpoint (similar to a PHP route)
@app.route('/get-scraped-data', methods=['GET'])
def scrape_and_serve_data():
    # Step 1: Web scraping (customize this to your target site)
    target_url = "https://example.com"  # Replace with your actual target
    try:
        # Send request to the target website
        response = requests.get(target_url, 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.raise_for_status()  # Throw error if request fails

        # Parse the HTML to extract data
        soup = BeautifulSoup(response.text, 'html.parser')
        scraped_items = []

        # Example: Extract all article titles and links (adjust selectors for your site)
        for article in soup.find_all('article'):
            title_tag = article.find('h2')
            link_tag = title_tag.find('a') if title_tag else None
            
            if title_tag and link_tag:
                scraped_items.append({
                    "title": title_tag.get_text(strip=True),
                    "link": link_tag['href']
                })

        # Step 2: Return data as JSON (just like PHP's json_encode)
        return jsonify({
            "status": "success",
            "data": scraped_items,
            "count": len(scraped_items)
        })

    except Exception as e:
        return jsonify({
            "status": "error",
            "message": str(e)
        }), 500

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000, debug=True)
2. Test the API

Run the script with python app.py, then:

  • Open your browser and go to http://your-server-ip:5000/get-scraped-data
  • You'll see a JSON response exactly like what your PHP API would return. Use tools like Postman to test different request methods if needed.
3. Connect Your Android App to the Python API

This part is identical to connecting to your PHP API! You'll use the same HTTP client (Volley, Retrofit, etc.) and parse the JSON response the same way.

Example with Volley:

// In your Android Activity/Fragment
String apiUrl = "http://your-python-server-ip:5000/get-scraped-data";
RequestQueue queue = Volley.newRequestQueue(this);

JsonObjectRequest jsonRequest = new JsonObjectRequest(
        Request.Method.GET,
        apiUrl,
        null,
        response -> {
            // Parse the JSON response (same as PHP API)
            try {
                String status = response.getString("status");
                if (status.equals("success")) {
                    JSONArray dataArray = response.getJSONArray("data");
                    for (int i = 0; i < dataArray.length(); i++) {
                        JSONObject item = dataArray.getJSONObject(i);
                        String title = item.getString("title");
                        String link = item.getString("link");
                        
                        // Add to your UI (e.g., RecyclerView adapter)
                        // yourAdapter.addItem(new ScrapedItem(title, link));
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        },
        error -> {
            // Handle errors (e.g., network issues)
            Log.e("API_ERROR", "Failed to fetch data: " + error.getMessage());
        }
);

queue.add(jsonRequest);
4. Key Tips for Production
  • Deployment: Replace Flask's built-in server with a production-ready option like Gunicorn, then use Nginx as a reverse proxy (similar to how you deploy PHP with Apache/Nginx).
  • Rate Limiting: Add delays or use proxies when scraping to avoid getting blocked by target websites. Always respect robots.txt rules.
  • Database Integration: If you need to store scraped data, use Python libraries like mysql-connector-python or SQLAlchemy to interact with databases—this works just like PHP's database extensions.

That's the gist of it! The workflow is identical to your PHP setup: scrape data, serve it via JSON endpoints, then consume that JSON in your Android app. Python just makes the scraping part a lot more straightforward with its robust libraries.

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

火山引擎 最新活动