python
import json
settings = json.load(open('settings.json', encoding='utf8'))
print(settings['key'])
settings.json
{
"key1" : "value1",
"key2" : "value2"
}
Use txt file as input data (store in array)
python
with open('file.txt', mode='r', encoding='utf-8') as f:
file = f.read()
files = file.split('\n')
print(files)
Use a txt file as output data (used in a for statement)
python
with open(filepath, mode='a', encoding='utf-8') as f:
f.write('String\n')
Copy file
python
import shutil
shutil.copy2(path_src, path_dist)
python
import datetime
now = datetime.datetime.now()
now_disp = dt_now.strftime('%Y%m%d')
python
file_list = os.listdir(path=filepath)
#An excerpt of a file containing a specific character from the array of files obtained above
file_list_unique = [s for s in file_list if 'Specified character string' in s]
terminal
pip install requests bs4
python
import requests
from bs4 import BeautifulSoup
url = 'website.com'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
# soup = BeautifulSoup(response.text, 'lxml')
# soup = BeautifulSoup(response.text, 'html5lib')
#Single acquisition
print(soup.select_one('.css-selector').text) #Get a string
print(soup.select_one('.css-selector').attrs['href']) #Get the value of an attribute
#If there are more than one`select`Get it with and turn it with for
htmls = soup.select('.css-selector')
for html in htmls:
print(html.select_one('.css-selector').text)
terminal
pip install openpyxl
python
import openpyxl
wb = openpyxl.load_workbook(filepath)
ws = wb['Sheet name']
#Get the value of a specific cell (A1 is row=1, column=1)
cell_obj = ws.cell(row=1, column=1).value
#Enter a value in a specific cell
ws.cell(row=1, column=1).value = 'value'
#Get all the values in a specific column (* Note: 0 is column A in the array index)
for cell_obj in list(ws.columns)[0]:
print(cell_obj.value)
#Get all the values in a specific row (* Note: 0 is column A in the array index)
for cell_obj in list(ws.rows)[0]:
print(cell_obj.value)
#Sheet save
wb.save(filepath)
Download chromedriver and place it directly under the project https://chromedriver.chromium.org/downloads
terminal
pip install selenium pyperclip
python
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
import pyperclip
#Chrome Driver settings
driver_path = './chromedriver'
options = Options()
options.add_argument('--window-size=1600,900')
driver = webdriver.Chrome(options=options, executable_path=driver_path)
IMPLICITLY_INTERVAL = 10
driver.implicitly_wait(IMPLICITLY_INTERVAL)
#Chrome Driver operation
##Show argument URL
driver.get('https://sample.com')
##Click the specified selector
driver.find_element_by_css_selector('.css-selector').click()
##Character input (send)_Since keys is slow, copy the character string with pyperclip and paste it)
pyperclip.copy('The character string you want to enter')
driver.find_element_by_css_selector('.css-selector').send_keys(Keys.CONTROL, 'v')
##Javascript execution (Example: Set the display property of the specified selector to none)
driver.execute_script('document.querySelector(".css-selector").style.display="none";')
##Quit Chrome Driver
driver.quit()
terminal
pip install pyinstaller
terminal
pyinstaller filename.py
pyinstaller filename.py --onefile #One.Put together in an exe file
pyinstaller filename.py --noconsole # .Do not show console when running exe
pyinstaller filename.py --onefile --noconsole #Both of the above
Recommended Posts