Python crawler self study series (2)

Look at the future 2021-01-21 03:29:33
python crawler self study series


 Insert picture description here

Preface

It's still a little chatter , Once again, bloggers are looking for a sense of existence .

Looking back , In the past, we talked about the simple operation of reptiles , And encapsulates a simple , Get the function of web page source data , It's good, isn't it .
Python Reptile self study series one

Today, we're going to grab the data we want from the web data we get .
( notes : Many things in this article have already been mentioned , So this article is basically a link , It won't be long )


HTML A brief introduction to the website

Xpath, God forever

XPath Is a kind of will XML The hierarchical structure of a document is described as a relational way . because HTML yes from XML The elements make up , So we can use XPath from HTML Locate and select elements in the document .

If you want to know more XPath Relevant knowledge , You can click on the blue on this side .


as for beautifulsoup Don't mention it .
Don't ask me why I didn't mention it , Look down and you'll see .


Performance comparison

 Insert picture description here

Do you realize ?


good , I've finished my knowledge , What is needed in the comparison is the code encapsulation in the actual combat of the project .

The actual combat of the project also has , Crawling 2021 Tencent school recruitment in

Let's look at it first. It's about , Then we come back to pick up some functions and encapsulate them .


Getting data from a web page

What about this function , Take the data straight away , But this Xpath The use of , It's not that easy .

def get_data(html_data,Xpath_path):
'''
This is a function to grab the required data from the web page source data
:param html_data: Web source data ( A single data )
:param Xpath_path: Xpath Addressing method
:return: A list of stored results
'''
data = html_data.content
data = data.decode().replace("<!--", "").replace("-->", "") # Delete comments from data 
tree = etree.HTML(data) # establish element object 
el_list = tree.xpath(Xpath_path)
return el_list

The one above is disposable , What about sustainable development ? For example, in a web page, you need to capture more than one type of data , That is to say, there are many sets of Xpath, What to do with that ?

I have two ways :
1. take element Object is used to transfer , The function is divided into two , see :

Sustainable development method 1 :

First step , Get the URL of element Object and return

# Get the URL of element object 
def get_element(html_data):
data = html_data.content
data = data.decode().replace("<!--", "").replace("-->", "")
tree = etree.HTML(data)
return tree

The second step , from element Object

def parser_element_data(Tree,Xpath):
el_list = Tree.xpath(Xpath)
return el_list

This method , It's a bit rustic , It really needs to be used , It's not very beautiful either , redundancy .

Let's look at method two .


Sustainable development method 2 :

What about this method , It's going to be all Xpath Pass in as a list , And then take the data through the loop .

def get_data_2(html_data,Xpath_path_list):
'''
Through multiple Xpath Extract data
:param html_data: Raw web data
:param Xpath_paths: Xpath Addressing list
:return: 2 d list , A kind of addressing data, a list
'''
el_data = []
data = html_data.content
data = data.decode().replace("<!--", "").replace("-->", "")
tree = etree.HTML(data)
for Xpath_path in Xpath_path_list:
el_list = tree.xpath(Xpath_path)
el_data.append(el_list)
el_list = [] # Let's clean it up for safety's sake 
return el_data

practice

This one is relatively short , But the content is not short .
If you have a heart, you can find a website to practice Xpath, Let's say recruitment website .


版权声明
本文为[Look at the future]所创,转载请带上原文链接,感谢
https://pythonmana.com/2021/01/20210121032906893B.html

  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