diff --git a/practice_projects/2048.py b/practice_projects/2048.py new file mode 100644 index 0000000..c28d294 --- /dev/null +++ b/practice_projects/2048.py @@ -0,0 +1,14 @@ +#!python3 +# 2048.py - auto play https://gabrielecirulli.github.io/2048/ + +from selenium import webdriver +from selenium.webdriver.common.keys import Keys + +browser = webdriver.Chrome() +browser.get("https://gabrielecirulli.github.io/2048/") +htmlElem = browser.find_element_by_tag_name('html') +while True: + htmlElem.send_keys(Keys.UP) + htmlElem.send_keys(Keys.RIGHT) + htmlElem.send_keys(Keys.DOWN) + htmlElem.send_keys(Keys.LEFT) \ No newline at end of file diff --git a/practice_projects/cmd_email.py b/practice_projects/cmd_email.py new file mode 100644 index 0000000..cb7274f --- /dev/null +++ b/practice_projects/cmd_email.py @@ -0,0 +1,22 @@ +#!python3 +# cmd_email.py - send email by command + +from selenium import webdriver +from time import sleep + +browser = webdriver.Chrome() +browser.get('https://ui.ptlogin2.qq.com/cgi-bin/login?style=9&appid=522005705&daid=4&s_url=https%3A%2F%2Fw.mail.qq.com%2Fcgi-bin%2Flogin%3Fvt%3Dpassport%26vm%3Dwsk%26delegate_url%3D%26f%3Dxhtml%26target%3D&hln_css=http%3A%2F%2Fmail.qq.com%2Fzh_CN%2Fhtmledition%2Fimages%2Flogo%2Fqqmail%2Fqqmail_logo_default_200h.png&low_login=1&hln_autologin=%E8%AE%B0%E4%BD%8F%E7%99%BB%E5%BD%95%E7%8A%B6%E6%80%81&pt_no_onekey=1') +sleep(2) +try: + qq = browser.find_element_by_id('u') + qq.send_keys('375575128') + password = browser.find_element_by_id('p') + password.send_keys('FzVSIPYk87890310') + go = browser.find_element_by_id('go') + go.click() + sleep(1) + password2 = browser.find_element_by_id('pwd') + password2.send_keys('Cpz87890310') + password2.submit() +except: + print('Was not able to find an element with your qq.') diff --git a/practice_questions/README.md b/practice_questions/README.md index fe82c8e..57d750d 100644 --- a/practice_questions/README.md +++ b/practice_questions/README.md @@ -9,4 +9,5 @@ - [Chapter 7 – Pattern Matching with Regular Expressions](ch07/README.md) - [Chapter 8 – Reading and Writing Files](ch08/README.md) - [Chapter 9 – Organizing Files](ch09/README.md) -- [Chapter 10 – Debugging](ch10/README.md) \ No newline at end of file +- [Chapter 10 – Debugging](ch10/README.md) +- [Chapter 11 – Web Scraping](ch11/README.md) \ No newline at end of file diff --git a/practice_questions/ch11/README.md b/practice_questions/ch11/README.md new file mode 100644 index 0000000..d6c981a --- /dev/null +++ b/practice_questions/ch11/README.md @@ -0,0 +1,81 @@ +# Chapter 11 – Web Scraping + +> Q: 1. Briefly describe the differences between the webbrowser, requests, BeautifulSoup, and selenium modules. + +`webbrowser` can launch a web browser to a specific URL by `open()`; +`requests` can download files and pages from the Web. +`beautifulSoup` module parses HTML. +`selenium` launch and control a browser. + +> Q: 2. What type of object is returned by requests.get()? How can you access the downloaded content as a string value? + +Response object. `getText()`. + +> Q: 3. What Requests method checks that the download worked? + +`raise_for_status()` + +> Q: 4. How can you get the HTTP status code of a Requests response? + +`status_code` + +> Q: 5. How do you save a Requests response to a file? + +```py +saveFile = open('SaveFile', 'wb') +for chunk in res.iter_content(100000): + saveFile.write(chunk) +saveFile.close() +``` + +> Q: 6. What is the keyboard shortcut for opening a browser’s developer tools? + +F12 + +> Q: 7. How can you view (in the developer tools) the HTML of a specific element on a web page? + +Inspect Element + +> Q: 8. What is the CSS selector string that would find the element with an id attribute of main? + +`#main` + +> Q: 9. What is the CSS selector string that would find the elements with a CSS class of highlight? + +`.highlight` + +> Q: 10. What is the CSS selector string that would find all the `
` elements inside another `
` element? + +`div div` + +> Q: 11. What is the CSS selector string that would find the `