1. 程式人生 > >25、Python之禪

25、Python之禪

sel tex idea col ase face 規則 ted flat

要求: 爬取網頁你好,蜘蛛俠!中的Python之禪中英文版本,並且打印。 目的: 練習使用selenium爬取動態網頁的信息。 練習selenium與BeautifulSoup的搭配使用。 URL https://localprod.pandateacher.com/python-manuscript/hello-spiderman/ 方法一: 用selenium
 1 from selenium import webdriver
 2 import time
 3 
 4 driver = webdriver.Chrome()
 5 
 6
driver.get(https://localprod.pandateacher.com/python-manuscript/hello-spiderman/) 7 time.sleep(2) 8 9 button = driver.find_element_by_class_name(sub) 10 button.click() 11 time.sleep(1) 12 13 python_zens = driver.find_elements_by_class_name(content) 14 15 for python_zen in python_zens:
16 print(python_zen.find_element_by_tag_name(h1).text,end=\n\n) 17 print(python_zen.find_element_by_tag_name(p).text,end=\n\n) 18 19 driver.close()
 1 The Zen of Python
 2 
 3 Beautiful is better than ugly.
 4 Explicit is better than implicit.
 5 Simple is better than complex.
6 Complex is better than complicated. 7 Flat is better than nested. 8 Sparse is better than dense. 9 Readability counts. 10 Special cases arent special enough to break the rules. 11 Although practicality beats purity. 12 Errors should never pass silently. 13 Unless explicitly silenced. 14 In the face of ambiguity, refuse the temptation to guess. 15 There should be one-- and preferably only one --obvious way to do it. 16 Although that way may not be obvious at first unless youre Dutch. 17 Now is better than never. 18 Although never is often better than *right* now. 19 If the implementation is hard to explain, its a bad idea. 20 If the implementation is easy to explain, it may be a good idea. 21 Namespaces are one honking great idea -- lets do more of those! 22 23 Python之禪 24 25 優美勝於醜陋 26 明了勝於晦澀 27 簡潔勝於復雜 28 復雜勝於淩亂 29 扁平勝於嵌套 30 間隔勝於緊湊 31 可讀性很重要 32 即便假借特例的實用性之名,也不可違背這些規則 33 不要包容所有錯誤,除非你確定需要這樣做 34 當存在多種可能,不要嘗試去猜測 35 而是盡量找一種,最好是唯一一種明顯的解決方案 36 雖然這並不容易,因為你不是 Python 之父 37 做也許好過不做,但不假思索就動手還不如不做 38 如果你無法向人描述你的方案,那肯定不是一個好方案;反之亦然 39 命名空間是一種絕妙的理念,我們應當多加利用
方法二:用selenium 和 BeautifulSoup
 1 from selenium import webdriver
 2 from bs4 import BeautifulSoup
 3 import time
 4 
 5 driver = webdriver.Chrome()
 6 
 7 driver.get(https://localprod.pandateacher.com/python-manuscript/hello-spiderman/)
 8 time.sleep(2)
 9 
10 button = driver.find_element_by_class_name(sub)
11 button.click()
12 time.sleep(1)
13 
14 pagesource = driver.page_source
15 
16 soup = BeautifulSoup(pagesource,html.parser)
17 items = soup.find_all(class_=content)
18 for item in items:
19     print(\n\t+item.find(h1).text)
20     print(item.find(p).text)
21 
22 driver.close()
 1         The Zen of Python
 2 
 3             Beautiful is better than ugly.
 4             Explicit is better than implicit.
 5             Simple is better than complex.
 6             Complex is better than complicated.
 7             Flat is better than nested.
 8             Sparse is better than dense.
 9             Readability counts.
10             Special cases arent special enough to break the rules.
11             Although practicality beats purity.
12             Errors should never pass silently.
13             Unless explicitly silenced.
14             In the face of ambiguity, refuse the temptation to guess.
15             There should be one-- and preferably only one --obvious way to do it.
16             Although that way may not be obvious at first unless youre Dutch.
17             Now is better than never.
18             Although never is often better than *right* now.
19             If the implementation is hard to explain, its a bad idea.
20             If the implementation is easy to explain, it may be a good idea.
21             Namespaces are one honking great idea -- lets do more of those!
22 
23         Python之禪
24 
25             優美勝於醜陋
26             明了勝於晦澀
27             簡潔勝於復雜
28             復雜勝於淩亂
29             扁平勝於嵌套
30             間隔勝於緊湊
31             可讀性很重要
32             即便假借特例的實用性之名,也不可違背這些規則
33             不要包容所有錯誤,除非你確定需要這樣做
34             當存在多種可能,不要嘗試去猜測
35             而是盡量找一種,最好是唯一一種明顯的解決方案
36             雖然這並不容易,因為你不是 Python 之父
37             做也許好過不做,但不假思索就動手還不如不做
38             如果你無法向人描述你的方案,那肯定不是一個好方案;反之亦然
39             命名空間是一種絕妙的理念,我們應當多加利用

25、Python之禪