首页 > 解决方案 > 如何从请求 python 的网站获取文本

问题描述

我正在尝试将我拍摄的图像转换为实际文字,并且我找到了一个很棒的网站来做到这一点。我正在尝试上传图像,从下拉菜单中选择一个值,然后点击“转换”。之后,我希望它从 textarea 中复制所有内容。但是,我尝试的一切都不起作用。请帮忙。这是我的代码:

import requests
from bs4 import BeautifulSoup as bs
import time

url = 'https://www.onlineocr.net/'
def oneFun():
    global url

    files = {'file': open('test.png', 'rb')}

    r = requests.post(url, files=files)
    print(r.status_code)

def twoFun():
    global url

    s = requests.Session()
    r = s.get(url)

    soup = bs(r.text, 'html.parser')

    dataload={
        '__EVENTTARGET': 'ctl00$MainContent$comboOutput',
        '__EVENTARGUMENT': '',
        '__LASTFOCUS': '',
        '__VIEWSTATE': soup.find('input',{'id':'__VIEWSTATE'}).attrs['value'],
        '__VIEWSTATEGENERATOR': soup.find('input',{'id':'__VIEWSTATEGENERATOR'}).attrs['value'],
        'ctl00$MainContent$comboOutput': 'Text Plain (txt)',
        'ctl00$MainContent$comboOutput': '',
        'ScrollTop':'' ,
        '__dnnVariable': ''
    }



    headers={
        'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
        'accept-encoding': 'gzip, deflate, br',
        'accept-language': 'en-US,en;q=0.9',
        'cache-control': 'max-age=0',
        'content-length': '3278',
        'Host': 'tikah.iscool.co.il',
        'Origin': 'http://tikah.iscool.co.il',
        'Referer': 'http://tikah.iscool.co.il/default.aspx',
        'upgrade-insecure-requests': '1',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36'

    }

    r = s.post(r.url,headers=headers,data=dataload)
    print(r.status_code)
def threeFun():
    values = { 'submit': 'CONVERT'}

    req = requests.post(url, data=values)
    print(req.status_code)


oneFun()
twoFun()
threeFun()

time.sleep(3)

#I want it to get and print the text here

print(text)

我正在上传 test.png 的网站,从下拉菜单中选择“纯文本 (txt)”选项,然后点击“转换”按钮。

标签: pythonpython-requests

解决方案


推荐阅读