借鉴本章:https://blog.youkuaiyun.com/aaronjny/article/details/80291997
import requests #加载模块(作用:发送网络请求);
from bs4 import BeautifulSoup #网页源码解析器与第三方库lxml一起使用加快速度 pip install lxml
re = requests.get('https://blog.youkuaiyun.com/qq_43595047')#接受网络请求返回的状态,
bo = BeautifulSoup(re.content,'lxml')#将网页源码构造成beautifulsoup的对象,方便操作!
a_list = bo.find_all('a') #获取网页中a标签对象
text = []#建立一个人空列表,用于存放数据!
for a in a_list:#for循环
href = a.get('href')#依次取出href->对象的连接地址;
text.append(href)#一次添加到列表中;
text.append('\n')#添加一次,换行,方便查看;
with open('try_info.txt','w') as f:#写入到文件中;
try :#没有报错就执行写入操作;
f.writelines(text)
except TypeError:#包键值空时,跳过操作;
pass