25、Python之禅

要求:

爬取网页你好,蜘蛛侠!中的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 aren‘t 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 you‘re Dutch.
17 Now is better than never.
18 Although never is often better than *right* now.
19 If the implementation is hard to explain, it‘s a bad idea.
20 If the implementation is easy to explain, it may be a good idea.
21 Namespaces are one honking great idea -- let‘s 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 aren‘t 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 you‘re Dutch.
17             Now is better than never.
18             Although never is often better than *right* now.
19             If the implementation is hard to explain, it‘s a bad idea.
20             If the implementation is easy to explain, it may be a good idea.
21             Namespaces are one honking great idea -- let‘s do more of those!
22
23         Python之禅
24
25             优美胜于丑陋
26             明了胜于晦涩
27             简洁胜于复杂
28             复杂胜于凌乱
29             扁平胜于嵌套
30             间隔胜于紧凑
31             可读性很重要
32             即便假借特例的实用性之名,也不可违背这些规则
33             不要包容所有错误,除非你确定需要这样做
34             当存在多种可能,不要尝试去猜测
35             而是尽量找一种,最好是唯一一种明显的解决方案
36             虽然这并不容易,因为你不是 Python 之父
37             做也许好过不做,但不假思索就动手还不如不做
38             如果你无法向人描述你的方案,那肯定不是一个好方案;反之亦然
39             命名空间是一种绝妙的理念,我们应当多加利用

原文地址:https://www.cnblogs.com/www1707/p/10850638.html

时间: 2024-08-27 12:05:18

25、Python之禅的相关文章

python之禅 --- import this

>>> import thisThe Zen of Python, by Tim Peters Beautiful is better than ugly.Explicit is better than implicit.Simple is better than complex.Complex is better than complicated.Flat is better than nested.Sparse is better than dense.Readability cou

python 之禅

想要真正深入了解一门语言,需要用心去感受.下面是python之禅,python的设计哲学,对于编程很有指导意义.(翻译部分摘自网络,同时自己有一些更改) >>> import this The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than c

(转)python之禅

凡是用过 Python的人,基本上都知道在交互式解释器中输入 import this 就会显示 Tim Peters 的 The Zen of Python,但它那偈语般的语句有点令人费解,所以我想分享一下我对它的体会,顺带给出我的翻译. The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. C

Python之禅中的几句话--传说中的蛇宗总纲

Simple is better than complex. 简洁胜于复杂. Now is better than never. Although never is often better than *right* now. 做也许好过不做,但不假思索就动手还不如不做. 放全文,来源已然不明,若有指明,必当感谢! The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than imp

Python之禅(原文、中文翻译、解释)

The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special

Python之禅,亦是人生之禅

C:\Users\Rage>python Python 3.6.3 (v3.6.3:2c5fed8, Oct 3 2017, 18:11:49) [MSC v.1900 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import this The Ze

python:python之禅

最近在学python,今晚看了一个名叫"python全栈之路系列"博客的关于python的相关博客,其中开篇就说到了python的设计哲学:优雅,简洁,高效... 可以在编译器里面输入如下语句来查看python语言的设计哲学: 1 >>> import this 2 The Zen of Python, by Tim Peters 3 4 Beautiful is better than ugly. 5 Explicit is better than implicit

Python之禅---1、什么是编程语言

1.编程与编程语言: Python是一门编程语言,首先需要搞清楚一下几点: 1.编程目的: #计算机的发明,是为了用机器取代/解放人力,而编程的目的则是将人类的思想流程按照某种能够被计算机识别的表达方式传递给计算机,从而达到让计算机能够像人脑/电脑一样自动执行的效果. 2.什么是编程语言: #上面提及的能够被计算机所识别的表达方式即编程语言,语言是沟通的介质,而编程语言是程序员与计算机沟通的介质.在编程的世界里,计算机更像是人的奴隶,人类编程的目的就命令奴隶去工作. 3.什么是编程:     #

Python之禅---2、python介绍和应用场景介绍

1,.python的由来 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,Guido开始写能够解释Python语言语法的解释器.Python这个名字,来自Guido所挚爱的电视剧Monty Python's Flying Circus.他希望这个新的叫做Python的语言,能符合他的理想:创造一种C和shell之间,功能全面,易学易用,可拓展的语言. 2.应用领域 Python可以应用于众多领域,如:数据分析.组件集成.网络服务.图像处理.数值计