运用re库的中的操作方法和技能提取自己感兴趣网页的超链接,并对过程和输出的结果进行展示。
时间: 2024-09-22 18:05:05 浏览: 35
在Python中,可以使用`re`库(Regular Expression,正则表达式)配合`requests`库来抓取并提取网页中的超链接。以下是一个简单的步骤:
首先,需要安装`requests`库,如果尚未安装,可以使用`pip install requests`命令。
```python
import re
import requests
# 获取网页内容
def get_html(url):
try:
response = requests.get(url)
response.raise_for_status() # 检查请求是否成功
return response.text
except requests.exceptions.RequestException as e:
print(f"Error occurred while fetching the page: {e}")
return None
# 使用正则表达式提取超链接
def extract_links(html):
pattern = r'<a\s+(?:[^>]*?\s+)?href="([^"]*)"[^>]*>'
links = re.findall(pattern, html)
return links
# 示例:获取并显示Python官网首页的超链接
url = "https://www.python.org/"
html_content = get_html(url)
if html_content:
extracted_links = extract_links(html_content)
print("提取到的Python官网超链接:")
for link in extracted_links:
print(link)
else:
print("无法获取页面内容")
阅读全文
相关推荐














