Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
415 views
in Technique[技术] by (71.8m points)

I want to copy all the text from a webpage and paste it to an word file. I am using selenium and python

This is my code

 import webbrowser as web
 from selenium import webdriver 
 from selenium.webdriver.common.keys import Keys
 import time
 import pyautogui
 import os
 import pyperclip
 import docx


 chrome_path = 'C:/Users/Jeet/Desktop/chromedriver.exe'
 custom_options = webdriver.ChromeOptions()
 prefs = {
          "translate_whitelists": {"uk":"en"},
          "translate":{"enabled":"true"}
          }
 custom_options.add_experimental_option("prefs", prefs)
 driver = webdriver.Chrome(chrome_path, options=custom_options)
 driver.get('https://zakupki.com.ua/tender/10019981')
 time.sleep(10)

driver.find_element_by_css_selector("body").send_keys(Keys.CONTROL + "a")
driver.find_element_by_css_selector("body").send_keys(Keys.CONTROL + "c")

os.system("start " + "demo.docx")

time.sleep(5)
a = pyperclip.paste()
pyautogui.typewrite(a)
time.sleep(5)}

This code is running but text is not copying properly in word file. what can be done to copy specific text (From Result to Customer Information) from the above given webpage

question from:https://stackoverflow.com/questions/65848492/i-want-to-copy-all-the-text-from-a-webpage-and-paste-it-to-an-word-file-i-am-us

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
import pyautogui

import os
import time
import pyperclip

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome()

driver.maximize_window()
        
driver.get(
    "https://www.facebook.com")
driver.find_element_by_css_selector("body").send_keys(Keys.CONTROL+"a")
driver.find_element_by_css_selector("body").send_keys(Keys.CONTROL+"c")

os.system("start " + "test.docx")

time.sleep(5)


a = pyperclip.paste()

pyautogui.typewrite(a)
time.sleep(5)

you can use pyperclip to get the clipboard content , os to open a text.docx and pyautogui to paste it

Note: make sure you have a file called test.docx in current directory


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...