Log in
Sign up
See new Tweets

Conversation

Simon Willison
@simonw
·
Dec 3, 2022
Using ChatGPT to write a scraper API for ChatGPT https://fedi.simonwillison.net/@simon/109451897151583419…
Me: Use Python and Playwright to build a Flask API which opens https://chat.openai.com/ and inserts text into the input box on it and then waits 10 seconds and returns the contents of the last div.ConversationItem__Message element on the page

from flask import Flask, jsonify
from playwright import sync_playwright

app = Flask(__name__)

@app.route('/insert_text', methods=['POST'])
def insert_text():
    text = request.form.get('text')

    with sync_playwright() as p:
        browser = p.chromium.launch()
        page = browser.new_page()
        page.goto('https://chat.openai.com/')
        input_box = page.query_selector('input[type="text"]')
        input_box.fill(text)
        page.wait_for_timeout(10000)
        message = page.query_selector('div.ConversationItem__Message')
        return jsonify({ 'message': message.inner_text })

if __name__ == '__main__':
    app.run()
read image description
ALT
5
24
143
Simon Willison
@simonw
Inspired by https://github.com/taranjeet/chatgpt-api/blob/c94d74b84b706174c8b6fd96c939fe4ad3ebbb5c/server.py… which is the same idea but should actually work, because it includes the authentication step that I skipped
github.com
chatgpt-api/server.py at c94d74b84b706174c8b6fd96c939fe4ad3ebbb5c · taranjeet/chatgpt-api
This repo is unofficial ChatGPT api. It is based on Daniel Gross's WhatsApp GPT - chatgpt-api/server.py at c94d74b84b706174c8b6fd96c939fe4ad3ebbb5c · taranjeet/chatgpt-api
9:43 PM · Dec 3, 2022
2
Retweets
21
Likes
Part-Time Entrepreneur
@pieracle
·
Dec 3, 2022
Replying to
@simonw
"Just came up with a new idea for a project