파이썬 도움되는 정보 1
- recursive iteration through nested json for specific key in python
- How to prettyprint a JSON file?
- 파이썬 패키지 백업 및 복구
- selenium wait
- Python Selenium: wait until an element is no longer stale?
- Python Selenium: How to let human interaction in a automated script?
- Open web in new tab Selenium + Python
- 주피터 노트북 설치 (방구석 잡코딩)
- 셀레늄으로 네이버 로그인하기 (방구석 잡코딩)
1. recursive iteration through nested json for specific key in python
def item_generator(json_input, lookup_key):
if isinstance(json_input, dict):
for k, v in json_input.items():
if k == lookup_key:
yield v
else:
yield from item_generator(v, lookup_key)
elif isinstance(json_input, list):
for item in json_input:
yield from item_generator(item, lookup_key)
2. How to prettyprint a JSON file?
https://stackoverflow.com/questions/12943819/how-to-prettyprint-a-json-file
import json
def pp_json(json_thing, sort=True, indents=4):
if type(json_thing) is str:
print(json.dumps(json.loads(json_thing), sort_keys=sort, indent=indents))
else:
print(json.dumps(json_thing, sort_keys=sort, indent=indents))
return None
pp_json(your_json_string_or_dict)
# 한글 출력 및 파일로 예쁘게 저장
with open("result.txt", "w", encoding="utf8") as f:
json.dump(json.loads(value), f, indent=4, ensure_ascii=False)
# json.loads : 스트링을 파라미터로 받아 리스트로 변환
# josn.dumps : 리스트를 파라미터로 받아 문자열로 변환
# json.load 와 json.dump 는 파일에 쓸 때 사용
3. 파이썬 패키지 백업 및 복구
# 파이썬 패키지 백업하고 한번에 설치하는 방법
https://www.youtube.com/watch?v=pgbDVPPtSB0
# 설치된 패키지 목록 보기
pip list
# 설치된 패키지 목록 보기 (조건 가능)
pip freeze
# 설치된 패키지 백업하기
pip freeze > backup.txt
# 복구하기
pip install -r backup.txt
# 파이썬 패키지 한번에 설치
pip install bs4, pillow, pyautogui
4. selenium wait
# Explicit Waits
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox()
driver.get("http://somedomain/url_that_delays_loading")
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "myDynamicElement"))
)
finally:
driver.quit()
# Implicit Waits
from selenium import webdriver
driver = webdriver.Firefox()
driver.implicitly_wait(10) # seconds
driver.get("http://somedomain/url_that_delays_loading")
myDynamicElement = driver.find_element_by_id("myDynamicElement")
5. Python Selenium: wait until an element is no longer stale?
# Python Selenium: wait until an element is no longer stale?
https://stackoverflow.com/questions/46968770/python-selenium-wait-until-an-element-is-no-longer-stale
element = driver.find_element_by_id("elementID")
# do something that refreshes the element
self.wait.until(EC.staleness_of(element))
element = self.wait.until(EC.visibility_of_element_located((By.ID, "elementID")))
# do something with element
# As you want to pick up the text property once the WebElement is no longer stale, you can use the following :
wait1 = WebDriverWait(driver, 10) wait1.until(expected_conditions.text_to_be_present_in_element((By.ID, "elementID"), "expected_text1")) # or wait2 = WebDriverWait(driver, 10) wait2.until(expected_conditions.text_to_be_present_in_element_value((By.ID, "elementID"), "expected_text2"))
6. Python Selenium: How to let human interaction in a automated script?
# Python Selenium: How to let human interaction in a automated script?
https://stackoverflow.com/questions/36725973/python-selenium-how-to-let-human-interaction-in-a-automated-script
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# load the page
driver.get("https://www.google.com/recaptcha/api2/demo")
# get the submit button
bt_submit = driver.find_element_by_css_selector("[type=submit]")
# wait for the user to click the submit button (check every 1s with a 1000s timeout)
WebDriverWait(driver, timeout=1000, poll_frequency=1) \
.until(EC.staleness_of(bt_submit))
print "submitted"
7. Open web in new tab Selenium + Python
# Open web in new tab Selenium + Python
https://stackoverflow.com/questions/28431765/open-web-in-new-tab-selenium-python
8. 주피터 노트북 설치 (방구석 잡코딩)
# 설치
pip install jupyter
# 실행
jupyter notebook
# Python 시작
폴더를 들어가서 New – Python 3
# 이름 변경
상단 이름 부분을 누룬다.
# 소스 실행
툴바 – Run
메뉴 – Cell – Run Cells
단축키 – Ctrl + Enter
단축키 – Alt + Enter (실행 후 아래에 셀 삽입)
# 기타
ESC 키 – 단축키 실행할 수 있는 상태
ENTER 키 – 편집 상태
단축키 – A : 위에 셀 삽입
단축키 – B : 아래에 셀 삽입
단축키 – X : 셀 삭제
무한 루프에 빠졌다면 : Kernal – Restart
9. 셀레늄으로 네이버 로그인하기 (방구석 잡코딩)
https://www.youtube.com/watch?v=6VtuLMb-lwY
from selenium import webdriver
driver = webdriver.Chrome("./chromedriver")
driver.get("https://www.naver.com")
driver.find_element_by_xpath('//*[@id="account"]/div/a').click()
driver.execute_script("document.getElementById('id').value = 'my_id'")
driver.execute_script("document.getElementById('pw').value = 'my_pw'")
driver.find_element_by_xpath('//*[@id="log.login"]').click()