python爬取指定网页的所有内容,findall正则表达式怎么匹配
时间: 2024-01-24 12:33:25 浏览: 130
你可以使用Python中的`requests`库来获取指定网页的内容,然后使用`re`模块中的`findall`函数来进行正则表达式的匹配。
下面是一个示例代码,演示如何使用`requests`和`re`库来实现爬取指定网页的所有内容并进行正则匹配:
```python
import requests
import re
# 指定要爬取的网页链接
url = 'https://www.example.com'
# 发送GET请求获取网页内容
response = requests.get(url)
content = response.text
# 使用正则表达式进行匹配
pattern = r'<div class="example">(.+?)</div>'
matches = re.findall(pattern, content)
# 输出匹配到的内容
for match in matches:
print(match)
```
在上述代码中,我们首先使用`requests.get()`函数发送GET请求获取网页的内容,然后将内容保存在`response.text`中。接下来,我们定义了一个正则表达式模式`pattern`,用于匹配包含`<div class="example">`标签的内容。最后,我们使用`re.findall()`函数对网页内容进行匹配,并将匹配到的结果保存在`matches`中。最后,我们遍历`matches`并输出每个匹配到的内容。
请注意,上述示例代码中的正则表达式模式仅供参考。你需要根据你要匹配的具体内容来编写适合的正则表达式模式。
阅读全文
相关推荐

















