注意:target.text能获取text的值,但是无法修改text值,要用target.string修改
from bs4 import BeautifulSoup
import os
with open("index.html","r",encoding="utf-8") as htmlfile:
html=htmlfile.read()
soup = BeautifulSoup(html, 'lxml') #生成BeautifulSoup对象
targets = soup.find_all('a') #所有名称为a的节点
for target in targets:
#此地不能用target.text
target.string="中卫日报("+target["href"][27:37]+")"
htfile=os.path.join(os.getcwd(),target["href"])
print(htfile)
#if os.path.exists(htfile):
# target["style"]="color:red;"
#else:
# target["style"]="color:green;"
with open("index.html","w") as fp:
fp.write(soup.prettify())
经测试网上教程可用,参考地址:
https://www.yilvhun.com/1038.html
因自己下载的是3.10.4,做链接的时候就统一用的310.
安装后效果:
存在问题:按照该方法安装后缺少ssl导致pip无法使用。
经网上查找到的解决方法:
1.编译安装openssl。系统自带的版本太低用不了,编译安装python已经吐了,这个再编译安装已经吐了,不再测试。
2.临时解决方案:
pip310 install --trusted-host mirrors.aliyun.com pytest -i http://mirrors.aliyun.com/pypi/simple/