Python crawler from getting started to giving up advanced usage of 03 | Python crawler

SunriseCai 2020-11-13 11:31:57
python crawler getting started giving


This blog is only for my spare time to record articles , Publish to , Only for users to read , If there is any infringement , Please let me know , I'll delete it .
This article is pure and wild , There is no reference to other people's articles or plagiarism . Insist on originality !!

Preface

Hello . Here is Python Reptiles from getting started to giving up series of articles . I am a SunriseCai.

This article mainly introduces Python Reptiles It may be used in the process Python Advanced Grammar .

Python Advanced functions

lambda expression ,Python The form of a special definition function , Use it to define a Anonymous functions .

  • grammar :lambda argument_list: expressionargument_list For the parameters passed in ,expression Expression for , This whole thing is called lambda function )

  • Basic example :

lambda_eg = lambda x,y : x*y # Output the product of the two values passed in
print(lambda_eg (2, 3)) # 6

lambda There are many uses , This article only introduces the frequently used usage .

Method describe
sorted() sorted() Function to sort all objects that can be iterated .
filter() For filtering sequence , Filter out the elements that do not meet the conditions , Return iterator
map() Every element in the parameter sequence calls the function , Return iterator
zip() Used to take iteratable objects as parameters , Package the corresponding elements into multiple tuples
  • sorted():
list_eg = [70, 45, 37, 127, 148, 26, 121]
sorted_eg = sorted(list_eg, key=lambda x: x) # There is no lambda The function returns the same result
print(sorted_eg) # [26, 37, 45, 70, 121, 127, 148]
# sorted_eg = sorted(list_eg, key=lambda x: x,reverse=True) # add to reverse=True, Output... In reverse order
# print(sorted_eg)[148, 127, 121, 70, 45, 37, 26]
  • filter():
list_eg = [70, 45, 37, 127, 148, 26, 121]
is_even = filter(lambda x: x % 2 == 0, list_eg) # If it is judged to be even, it will output
print(list(is_even )) # [70, 148, 26] Because the output is an iterator , So convert to list Then the output
  • map():
list_eg = [70, 45, 37, 127, 148, 26, 121]
square = map(lambda x: x ** 2, list_eg) # Output square
print(list(square))
  • zip():
values = [70, 45, 37, 127, 148, 26, 121]
dogs = [' Siberian Husky ', ' Samoye ', ' Poodle ', ' Golden hair ', ' Shepherd Dog ', ' Ji Wawa ', ' corgi ']
dict_dogs = zip(dogs, values) # Traversing two sets of elements , Return a tuple
print(dict(dict_dogs)) # Convert to dictionary and output
# {
' Siberian Husky ': 70, ' Samoye ': 45, ' Poodle ': 37, ' Golden hair ': 127, ' Shepherd Dog ': 148, ' Ji Wawa ': 26, ' corgi ': 121}

Python The derived type

Derivation provides a simple way to create other data structures from one data sequence . In other words, you can Reduce the amount of code . There are three kinds of derivation , Namely List derivation Dictionary derivation as well as Set derivation . Let's see how they use it .

  • Python List derivation (list)
  • To create a 10 Divisible within 2 For example :

Conventional writing :

values = [70, 45, 37, 127, 148, 26, 121]
list_even = []
for i in values:
if i % 2 == 0:
list_even.append(i)
print(list_even) # [70, 148, 26]

List derivation :

values = [70, 45, 37, 127, 148, 26, 121]
list_even = list(i for i in values if i % 2 == 0)
print(list_even) # [70, 148, 26]

  • Python Dictionary derivation (dict)
  • Use two lists as key value pairs , Set up a dictionary as an example .

Conventional writing :

dogs = [' Siberian Husky ', ' Samoye ', ' Poodle ', ' Golden hair ', ' Shepherd Dog ', ' Ji Wawa ', ' corgi ']
values = [70, 45, 37, 127, 148, 26, 121]
dict_dogs = {
}
for k, v in zip(values, dogs):
dict_dogs[k] = v
print(dict_dogs)
# {
' Siberian Husky ': 70, ' Samoye ': 45, ' Poodle ': 37, ' Golden hair ': 127, ' Shepherd Dog ': 148, ' Ji Wawa ': 26, ' corgi ': 121}

Dictionary derivation :

dogs = [' Siberian Husky ', ' Samoye ', ' Poodle ', ' Golden hair ', ' Shepherd Dog ', ' Ji Wawa ', ' corgi ']
values = [70, 45, 37, 127, 148, 26, 121]
dict_dogs = {
i[0]: i[1] for i in zip(dogs, values)}
print(dict_dogs)
# {
' Siberian Husky ': 70, ' Samoye ': 45, ' Poodle ': 37, ' Golden hair ': 127, ' Shepherd Dog ': 148, ' Ji Wawa ': 26, ' corgi ': 121}

  • Python Set derivation (set)
  • To square the values in a list , Input into the set and output

Conventional writing :

values = [70, 45, 37, 127, 148, 26, 121]
square = []
for i in values:
square.append(i ** 2) # Square and store in a new list
set_square = set(square) # List to collection
print(set_square) # {
16129, 4900, 676, 2025, 21904, 14641, 1369}

Set derivation :

values = [70, 45, 37, 127, 148, 26, 121]
set_square = {
i ** 2 for i in values}
print(set_square) # {
16129, 4900, 676, 2025, 21904, 14641, 1369}

Through the above derivation example , Do you think derivation can reduce a lot of code !!

Other commonly used

Here are a few examples . Namely

Method 1 Method 2
The dictionary is based on value Sort The dictionary is based on key Sort
  • Dictionaries (dict) Sort , Sort by key sorted
  • Pay attention to the : If the dictionary (dict) The key value of is a character, not an integer , Use this time int(str) Convert to an integer .

The dictionary is based on key Sort :

dict_dogs = {
31: ' Siberian Husky ', 20: ' Samoye ', 3: ' Poodle ', 24: ' Golden hair '}
dict_order_dogs = dict(sorted(dict_dogs.items(), key=lambda x: x[0])) # positive sequence
dict_order_dogs = dict(sorted(dict_dogs.items(), key=lambda x: x[0], reverse=True)) # In reverse order
print(dict_order_dogs) # {
3: ' Poodle ', 20: ' Samoye ', 24: ' Golden hair ', 31: ' Siberian Husky '}

The dictionary is based on value Sort :

dict_dogs = {
' Siberian Husky ': 70, ' Samoye ': 45, ' Poodle ': 37, ' Golden hair ': 127}
dict_order_dogs = dict(sorted(dict_dogs.items(), key=lambda x: x[1])) # The order
dict_order_dogs = dict(sorted(dict_dogs.items(), key=lambda x: x[1], reverse=True)) # In reverse order
print(dict_order_dogs) # {
' Golden hair ': 127, ' Siberian Husky ': 70, ' Samoye ': 45, ' Poodle ': 37}

  • I will continue to add more grammar later .

above , It's all about this chapter .
In the actual programming process , There are countless grammars that can be used , be too numerous to enumerate , Only a few of the commonly used grammar are recorded here . During programming , You can use all kinds of grammar flexibly !!


Finally, I will summarize the content of this chapter :

  1. It introduces lambda Use of functions
  2. It introduces Python Three The derived type Use
  3. It introduces Python According to the dictionary (dict) Of key or value Sort

sunrisecai

  • Thank you for your patience in watching , Focus , Neverlost .
  • For the convenience of chicken pecking each other , Welcome to join QQ Group organization :648696280( There is no learning material in it , Just for questions )

Next article , be known as 《Python Reptiles from getting started to giving up 04 | Python The first crawler hit the request page 》.

版权声明
本文为[SunriseCai]所创,转载请带上原文链接,感谢

  1. 利用Python爬虫获取招聘网站职位信息
  2. Using Python crawler to obtain job information of recruitment website
  3. Several highly rated Python libraries arrow, jsonpath, psutil and tenacity are recommended
  4. Python装饰器
  5. Python实现LDAP认证
  6. Python decorator
  7. Implementing LDAP authentication with Python
  8. Vscode configures Python development environment!
  9. In Python, how dare you say you can't log module? ️
  10. 我收藏的有关Python的电子书和资料
  11. python 中 lambda的一些tips
  12. python中字典的一些tips
  13. python 用生成器生成斐波那契数列
  14. python脚本转pyc踩了个坑。。。
  15. My collection of e-books and materials about Python
  16. Some tips of lambda in Python
  17. Some tips of dictionary in Python
  18. Using Python generator to generate Fibonacci sequence
  19. The conversion of Python script to PyC stepped on a pit...
  20. Python游戏开发,pygame模块,Python实现扫雷小游戏
  21. Python game development, pyGame module, python implementation of minesweeping games
  22. Python实用工具,email模块,Python实现邮件远程控制自己电脑
  23. Python utility, email module, python realizes mail remote control of its own computer
  24. 毫无头绪的自学Python,你可能连门槛都摸不到!【最佳学习路线】
  25. Python读取二进制文件代码方法解析
  26. Python字典的实现原理
  27. Without a clue, you may not even touch the threshold【 Best learning route]
  28. Parsing method of Python reading binary file code
  29. Implementation principle of Python dictionary
  30. You must know the function of pandas to parse JSON data - JSON_ normalize()
  31. Python实用案例,私人定制,Python自动化生成爱豆专属2021日历
  32. Python practical case, private customization, python automatic generation of Adu exclusive 2021 calendar
  33. 《Python实例》震惊了,用Python这么简单实现了聊天系统的脏话,广告检测
  34. "Python instance" was shocked and realized the dirty words and advertisement detection of the chat system in Python
  35. Convolutional neural network processing sequence for Python deep learning
  36. Python data structure and algorithm (1) -- enum type enum
  37. 超全大厂算法岗百问百答(推荐系统/机器学习/深度学习/C++/Spark/python)
  38. 【Python进阶】你真的明白NumPy中的ndarray吗?
  39. All questions and answers for algorithm posts of super large factories (recommended system / machine learning / deep learning / C + + / spark / Python)
  40. [advanced Python] do you really understand ndarray in numpy?
  41. 【Python进阶】Python进阶专栏栏主自述:不忘初心,砥砺前行
  42. [advanced Python] Python advanced column main readme: never forget the original intention and forge ahead
  43. python垃圾回收和缓存管理
  44. java调用Python程序
  45. java调用Python程序
  46. Python常用函数有哪些?Python基础入门课程
  47. Python garbage collection and cache management
  48. Java calling Python program
  49. Java calling Python program
  50. What functions are commonly used in Python? Introduction to Python Basics
  51. Python basic knowledge
  52. Anaconda5.2 安装 Python 库(MySQLdb)的方法
  53. Python实现对脑电数据情绪分析
  54. Anaconda 5.2 method of installing Python Library (mysqldb)
  55. Python implements emotion analysis of EEG data
  56. Master some advanced usage of Python in 30 seconds, which makes others envy it
  57. python爬取百度图片并对图片做一系列处理
  58. Python crawls Baidu pictures and does a series of processing on them
  59. python链接mysql数据库
  60. Python link MySQL database