python - 如何从请求 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)”选项,然后点击“转换”按钮。
解决方案
推荐阅读
- powershell - 使用 Powershell 从 Txt 文件和变量中搜索值
- azure - 我使用 Azure 搜索,而后者又使用 Lucene。有没有办法可以搜索此查询: colo?r 会导致获取“颜色”和“颜色”值?
- python - 连接到 USB 设备 python libusb 时出错
- python - django admin 的 list_editable 中外键字段的自定义查询集
- python - 对数组 2d python 中的每一行求和
- lua - IntValue 更改但未显示
- sql - 无法将函数参数解析为内部查询?
- ipu - V-IPU 软件在哪里?
- c++ - 大型项目的 C++ 结构化单元测试
- python - Boost/Python 快速入门示例不构建 Windows 10