I constantly need to copy some text data from the same website. Copying it myself is tedious and what I would love to have is: When I enter that website, and this particular element (specified with XPath) has some text in it (it's not blank) then the script gets Text from this element and copies it to my clipboard.
It would be great if it would work in my Chrome browser. Is it something I could achieve with Python and Selenium for example?
If you are just web scrapping then you really don't need selenium unless you need to input something or can't get there with just the URL. Just get the HTML and extract what you need.
import pyperclip
import time
from bs4 import BeautifulSoup
import requests
def get_page_html(url):
headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36"}
page = requests.get(url, headers=headers)
return page.content
def parse_html(url):
page_contents = get_page_html(url)
soup = BeautifulSoup(page_contents, 'html.parser')
divs = soup.find("div", {"class": "fulfillment-fulfillment-summary"})
return divs.encode_contents()
pyperclip.copy(parse_html(<URL>))
s = pyperclip.paste()#If needed