使用Python x100构建比BeautifulSoup更快的网络爬虫

2021年11月10日17:20:12 发表评论 856 次浏览

Python构建更快的网络爬虫:网络爬虫是一种从网页中提取结构化信息的技术。借助 Python,你可以使用BeautifulSouprequests和其他库构建高效的网络抓取工具。但是,这些解决方案还不够快。在本文中,我将向你展示一些使用 Python构建超快速网络爬虫的技巧。

不要使用 BeautifulSoup4 如何构建更快的网络爬虫?

BeautifulSoup4 友好且用户友好,但速度并不快。即使你使用外部提取器(例如lxml用于 HTML 解析或用于cchardet检测编码),它仍然很慢。

使用 selectolax 代替 BeautifulSoup4 进行 HTML 解析 #

selectolax是对ModestLexbor引擎的 Python 绑定。

Python构建更快的网络爬虫selectolax使用 pip安装:

pip install selectolax

更快的网络爬虫实现示例:用法selectolax类似于BeautifulSoup4

from selectolax.parser import HTMLParser

html = """
<body>
    <h1 class='>Welcome to selectolax tutorial</h1>
    <div id="text">
        <p class='p3'>Lorem ipsum</p>
        <p class='p3'>Lorem ipsum 2</p>
    </div>
    <div>
        <p id='stext'>Lorem ipsum dolor sit amet, ea quo modus meliore platonem.</p>
    </div>
</body>
"""
# Select all elements with class 'p3'
parser = HTMLParser(html)
parser.select('p.p3')

# Select first match
parser.css_first('p.p3')

# Iterate over all nodes on the current level
for node in parser.css('div'):
    for cnode in node.iter():
        print(cnode.tag, cnode.html)

有关更多信息,请访问selectolax 演练教程

更快的网络爬虫实现示例:使用 httpx 而不是请求 #

如何构建更快的网络爬虫?Pythonrequests是人类的 HTTP 客户端。它易于使用,但速度并不快。它只支持同步请求。

httpx是 Python 3 的全功能 HTTP 客户端,提供同步和异步 API,并支持 HTTP/1.1 和 HTTP/2。默认情况下,它提供标准的同步 API,但如果需要,还可以为你提供异步客户端选项。httpx使用 pip安装:

pip install httpx

httpx提供相同的api requests

import httpx
async def main():
    async with httpx.AsyncClient() as client:
        response = await client.get('https://httpbin.org/get')
        print(response.status_code)
        print(response.json())

import asyncio
asyncio.run(main())

示例和用法请访问httpx主页

使用 aiofiles 进行文件 IO #

Python构建更快的网络爬虫:aiofiles是一个基于 asyncio 的文件 I/O 的 Python 库。它提供了一个用于处理文件的高级 API。aiofiles使用 pip安装:

pip install aiofiles

更快的网络爬虫实现示例:基本用法:

import aiofiles
async def main():
    async with aiofiles.open('test.txt', 'w') as f:
        await f.write('Hello world!')

    async with aiofiles.open('test.txt', 'r') as f:
        print(await f.read())

import asyncio
asyncio.run(main())

有关更多信息,请访问aiofiles 存储库

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: