web页面元素提取,编写代码,提取图像URL。只用print实现如下输出: 图像列表中,png图像文件为:20180112025424524.png 对应的URL为:http://image.nationalgeographic.com.cn/2018/0112/20180112025424524.png 的python代码
时间: 2024-01-01 16:06:57 浏览: 140
以下是使用Python和BeautifulSoup库提取页面元素并打印输出图像URL的代码:
```python
import requests
from bs4 import BeautifulSoup
# 发送HTTP请求
url = 'http://www.example.com'
page = requests.get(url)
# 解析HTML页面
soup = BeautifulSoup(page.content, 'html.parser')
# 提取图像元素
image_elements = soup.find_all('img')
# 遍历图像元素并提取URL
for element in image_elements:
src = element.get('src')
if src.endswith('.png'):
print('图像列表中,png图像文件为:' + src)
print('对应的URL为:' + url + src)
```
请注意,此代码仅适用于示例网站。对于其他网站,您可能需要调整代码以正确提取图像元素和URL。
阅读全文
相关推荐





