python爬取div中的汉字,并以txt输出
时间: 2024-02-17 09:55:04 浏览: 26
以下是一个简单的Python爬虫程序,可以爬取一个网页中所有div标签中的汉字,并将其保存到一个txt文件中。
```python
import requests
from bs4 import BeautifulSoup
url = "http://example.com"
html = requests.get(url).text
soup = BeautifulSoup(html, 'html.parser')
# 找到所有的div标签
divs = soup.find_all('div')
# 遍历每个div标签,将其中的汉字存入一个列表
chinese_chars = []
for div in divs:
chinese_chars += [char for char in div.get_text() if '\u4e00' <= char <= '\u9fff']
# 将列表转换为字符串,并保存到一个txt文件中
with open('chinese_chars.txt', 'w', encoding='utf-8') as f:
f.write(''.join(chinese_chars))
```
以上程序使用了requests库和BeautifulSoup库来获取网页内容和解析HTML代码。首先,程序读取一个网页并将其转换为BeautifulSoup对象。然后,它找到所有的div标签,并遍历每个标签以提取汉字。最后,程序将提取到的汉字列表转换为字符串,并将其保存到一个txt文件中。
相关问题
python爬取div中的汉字,需要去掉div下其他的div
可以使用BeautifulSoup库来解析HTML文档,然后通过遍历div标签及其子标签的方式来获取其中的汉字。具体实现如下:
```python
from bs4 import BeautifulSoup
html = """
<div class="content">
这里是需要获取的汉字
<div class="other">其他div</div>
<div class="another">另一个div</div>
</div>
"""
soup = BeautifulSoup(html, 'html.parser')
content_div = soup.find('div', {'class': 'content'})
# 遍历content_div标签及其子标签,获取其中的汉字
chinese_chars = ''
for tag in content_div.descendants:
if tag.name is None and tag.strip():
chinese_chars += tag.strip()
print(chinese_chars) # 输出:这里是需要获取的汉字
```
首先,使用BeautifulSoup将HTML文档解析成树形结构,然后通过`find()`方法找到`class`属性为`content`的`div`标签。接着,使用`descendants`属性遍历`content_div`标签及其子标签,对于每一个非标签节点(即没有`name`属性的节点),如果其包含文本内容,则将其中的汉字添加到`chinese_chars`字符串中。最后,输出`chinese_chars`字符串即可。
python爬取创世中文网
您可以使用 Python 中的 requests 库和 BeautifulSoup 库来爬取创世中文网。具体步骤如下:
1. 导入 requests 和 BeautifulSoup 库:
```python
import requests
from bs4 import BeautifulSoup
```
2. 发送 GET 请求获取网页内容:
```python
url = 'https://www.chuangshi.cn/'
response = requests.get(url)
```
3. 解析网页内容,获取需要的信息:
```python
soup = BeautifulSoup(response.text, 'html.parser')
# 获取小说列表
novel_list = soup.find_all('div', class_='novel-item')
for novel in novel_list:
# 获取小说名称
name = novel.find('h4').text.strip()
# 获取小说作者
author = novel.find('p', class_='author').text.strip()
# 获取小说简介
intro = novel.find('p', class_='intro').text.strip()
print(name, author, intro)
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](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)