用Python获取网页https://www.qqxiuzi.cn/hanzi/gfhz/8105.html中的通用规范汉字,并写入文件
时间: 2023-12-23 14:03:16 浏览: 31
可以使用requests和BeautifulSoup库来获取网页内容和解析HTML,然后使用正则表达式来匹配汉字,最后将匹配到的汉字写入文件中。
以下是示例代码:
```python
import requests
from bs4 import BeautifulSoup
import re
# 发送请求获取网页内容
url = "https://www.qqxiuzi.cn/hanzi/gfhz/8105.html"
response = requests.get(url)
html = response.content.decode("utf-8")
# 解析HTML
soup = BeautifulSoup(html, "html.parser")
content = soup.find("div", class_="content")
# 匹配汉字
pattern = re.compile(r"[\u4e00-\u9fa5]")
result = pattern.findall(content.text)
characters = set(result)
# 写入文件
with open("characters.txt", "w", encoding="utf-8") as f:
for ch in characters:
f.write(ch)
```
运行以上代码后,会在当前目录下创建一个名为“characters.txt”的文件,其中包含网页中出现的所有通用规范汉字。
相关问题
用Python把网页https://www.qqxiuzi.cn/hanzi/gfhz/8105.html中的通用规范汉字,用网页http://www.yedict.com/zsbs.htm中的部首分析,取得每个汉字的部首,并写入文件
首先,我们需要用Python爬取这两个网页的内容。可以使用requests和BeautifulSoup库来实现。
```python
import requests
from bs4 import BeautifulSoup
# 爬取通用规范汉字网页
url1 = "https://www.qqxiuzi.cn/hanzi/gfhz/8105.html"
r1 = requests.get(url1)
soup1 = BeautifulSoup(r1.content, "html.parser")
# 爬取部首分析网页
url2 = "http://www.yedict.com/zsbs.htm"
r2 = requests.get(url2)
soup2 = BeautifulSoup(r2.content, "html.parser")
```
接着,我们需要从部首分析网页中提取每个汉字的部首。可以使用正则表达式来匹配部首信息。
```python
import re
# 获取部首信息
bs_dict = {}
for tr in soup2.find_all("tr"):
td_list = tr.find_all("td")
if len(td_list) >= 2:
hanzi = td_list[0].text.strip()
bushou = re.findall(r"[\u2E80-\u9FFF]", td_list[1].text)
if len(bushou) > 0:
bs_dict[hanzi] = bushou[0]
```
现在,我们可以遍历通用规范汉字网页中的每个汉字,取得它的部首信息,并写入文件。
```python
# 写入文件
with open("output.txt", "w", encoding="utf-8") as f:
for tr in soup1.find_all("tr"):
td_list = tr.find_all("td")
if len(td_list) >= 2:
hanzi = td_list[0].text.strip()
if hanzi in bs_dict:
bushou = bs_dict[hanzi]
f.write(f"{hanzi}\t{bushou}\n")
```
最终,我们得到一个名为output.txt的文件,其中每一行表示一个汉字和它的部首,以制表符分隔。
用Pytnon在网页http://code.web.idv.hk/charset/csws1.php上获取通用规范汉字(TGhanzi.txt)每个汉字的部首,并写入文件
首先,需要用requests库从网页上获取TGhanzi.txt文件的内容。然后,需要用正则表达式从文件中提取每个汉字,并用字典记录每个汉字的部首。最后,将字典写入文件。
以下是示例代码:
```python
import requests
import re
# 获取TGhanzi.txt文件内容
url = 'http://code.web.idv.hk/charset/csws1.php?charset=TGhanzi'
response = requests.get(url)
content = response.text
# 用正则表达式提取每个汉字
pattern = re.compile(r'[\u4e00-\u9fa5]')
hanzi_list = pattern.findall(content)
# 记录每个汉字的部首
bushou_dict = {}
for hanzi in hanzi_list:
url = f'http://xh.5156edu.com/html3/{ord(hanzi):X}.html'
response = requests.get(url)
content = response.text
pattern = re.compile(r'<a href="http://xh.5156edu.com/html3/\d+.html">(.*?)</a>')
bushou = pattern.search(content).group(1)
bushou_dict[hanzi] = bushou
# 将字典写入文件
with open('bushou.txt', 'w', encoding='utf-8') as f:
for hanzi, bushou in bushou_dict.items():
f.write(f'{hanzi}\t{bushou}\n')
```
运行以上代码后,将在当前目录下生成一个名为bushou.txt的文件,每行表示一个汉字和它的部首,用制表符分隔。
相关推荐
![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)