使用requests和bs4
时间: 2024-06-14 07:04:39 浏览: 12
以下是使用requests和bs4的示例代码:
```python
import requests
from bs4 import BeautifulSoup
# 发送请求并获取网页内容
url = 'https://cd.zu.ke.com/zufang'
response = requests.get(url)
# 解析网页内容
soup = BeautifulSoup(response.text, 'html.parser')
# 获取特定元素的内容
title = soup.title.text
print("网页标题:", title)
# 获取所有链接
links = soup.find_all('a')
for link in links:
print(link.get('href'))
# 获取特定元素的属性值
image = soup.find('img')
if image:
src = image.get('src')
print("图片链接:", src)
else:
print("未找到图片")
# 提取特定元素的文本内容
paragraphs = soup.find_all('p')
for p in paragraphs:
print(p.text)
# 提取特定元素的父元素
parent = soup.find('a').parent
print("链接的父元素:", parent)
# 提取特定元素的兄弟元素
siblings = soup.find('a').next_siblings
for sibling in siblings:
print(sibling)
# 提取特定元素的子元素
children = soup.find('div').children
for child in children:
print(child)
# 提取特定元素的下一个元素
next_element = soup.find('a').next_element
print("链接的下一个元素:", next_element)
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)