try to finished ch11

This commit is contained in:
pezy
2017-11-10 18:19:24 +08:00
parent 4c7cd73626
commit db264a0b93
4 changed files with 119 additions and 1 deletions

14
practice_projects/2048.py Normal file
View File

@@ -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)

View File

@@ -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.')

View File

@@ -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)
- [Chapter 10 Debugging](ch10/README.md)
- [Chapter 11 Web Scraping](ch11/README.md)

View File

@@ -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 browsers 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 `<div>` elements inside another `<div>` element?
`div div`
> Q: 11. What is the CSS selector string that would find the `<button>` element with a value attribute set to favorite?
`button[value='favorite']`
> Q: 12. Say you have a Beautiful Soup Tag object stored in the variable spam for the element `<div>`Hello world!`</div>`. How could you get a string 'Hello world!' from the Tag object?
spam.getText()
> Q: 13. How would you store all the attributes of a Beautiful Soup Tag object in a variable named linkElem?
linkElem.attrs
> Q: 14. Running import selenium doesnt work. How do you properly import the selenium module?
`from selenium import webdriver`
> Q: 15. Whats the difference between the find_element_* and find_elements_* methods?
one vs a list.
> Q: 16. What methods do Seleniums WebElement objects have for simulating mouse clicks and keyboard keys?
`click()` and `send_keys()`
> Q: 17. You could call send_keys(Keys.ENTER) on the Submit buttons WebElement object, but what is an easier way to submit a form with Selenium?
`submit()`
> Q: 18. How can you simulate clicking a browsers Forward, Back, and Refresh buttons with Selenium?
`forward()`, `back()`, `refresh()`