Python Selenium更改文本大小(缩放?设置?...) [英] Python Selenium Change Texts Size (Zoom?Setting?...)
问题描述
我有一个网页,我需要先截图然后再使用OCR解析其中的文本.如果我放大(Mac:command +'='),OCR的性能可以大大提高.所以我想知道如何在Python中使用Selenium进行放大/缩小.
I have a webpage that I need to take the screen shot first and then use OCR to parse out the texts inside. The performance of OCR could be dramatically improved if I zoom in(Mac: command + '='). So I am wondering how could I zoom in/out using selenium in Python.
有一个类似的帖子,但它们仅包含实现Java和C#中,但目标与我的相同.
There is a similar post but they only have the implementations in Java and C#, but the goal is the same as mine.
放大/缩小硒只是我的想法之一.以提高性能.我知道可能有几种实现方式.以下只是我的想法,我从未成功地实现它们.如果您可以证明它们可以工作并更改字体大小,我也可以接受作为答案.
Zoom in/out in selenium is just one of my thoughts. To improve the performance. I know there might be several ways to implement. Below are just my thoughts and I never successfully implement them. If you could prove them work and change the font size, I would also accept as answer.
-
也许可以更改浏览器的设置,然后将其另存为Chrome配置文件,因此,下次,我可以调用该配置文件,并且应该在整个过程中保留"ZOOM"设置,而无需进行任何操作.但是,似乎python selenium软件包不支持加载chrome配置文件,但是,它可以加载firefox配置文件. 链接
也许将屏幕截图作为矢量图像,所以请使用PIL等分别放大字体大小.
Maybe take the screen shot as a vector image, so use PIL etc. to amplify the font size separately.
...
非常感谢这篇文章和示例代码,可以帮助您入门!
Thanks a lot for the post and the a sample code to get you started!
#!/usr/bin/python
from selenium import webdriver
def main():
browser = webdriver.Chrome() # Sorry, I have to use Chrome, [chromedriver][3] is required
browser.set_window_size(1000, 1000)
browser.get("https://stackoverflow.com/users/1953475/b-mr-w")
# Fill in Your Magic Here to Make the Font Size Big!
browser.get_screenshot_as_file('/tmp/screenshot.png')
if __name__ == '__main__':
main()
推荐答案
您可以通过execute_script("$('#content').css('zoom', 5);")
缩放content
div:
You can zoom the content
div via execute_script("$('#content').css('zoom', 5);")
:
#!/usr/bin/python
from selenium import webdriver
import time
def main():
browser = webdriver.Chrome()
browser.set_window_size(1000, 1000)
browser.get("http://stackoverflow.com/users/1953475/b-mr-w")
browser.execute_script("$('#content').css('zoom', 5);")
time.sleep(5)
browser.get_screenshot_as_file('screenshot.png')
if __name__ == '__main__':
main()
但是,缩放存在问题:get_screenshot_as_file
不会向您显示整个页面-它会根据看到的内容(带有滚动条)制作图像.
But, there is a problem with zooming: get_screenshot_as_file
won't show you the whole page - it'll make an image from what it sees (with scrolls).
为什么在这里需要OCR?如何使用 html2text 模块获取文本?
Why do you need an OCR here? What about getting the text using html2text module?
希望有帮助.
这篇关于Python Selenium更改文本大小(缩放?设置?...)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!