import requests import re from bs4 import BeautifulSoup def get_html(url): headers = { 'User-Agent': 'Mozilla/5.0(Macintosh; Intel Mac OS X 10_11_4)\ AppleWebKit/537.36(KHTML, like Gecko) Chrome/52 .0.2743. 116 Safari/537.36' } # 模拟浏览器访问 response = requests.get(url, headers=headers) # 请求访问网站 html = response.text # 获取网页源码 return html # 返回网页源码 soup = BeautifulSoup(get_html('https://www.jianshu.com/'), 'lxml') # 初始化BeautifulSoup库,并设置解析器 print(get_html('https://www.jianshu.com/')) for li in soup.find_all(name='li'): # 遍历父节点 for a in li.find_all(name='a'): # 遍历子节点 if a.string == None: pass else: print(a.string) # 输出结果
output
前台妹子:没有学历有多现实
张二真实故事驿站
十八岁那年,我被“包养”了。
江蓠子
大型产品经理PUA现场
明天上线
用户故事地图的使用方法
小hoho
做好自己
直着蓝
阴盛阳衰的单位,是种灾难
金错刀618
把一件事情持续做三年是一种什么样的体验呢?
践侠客
后期更新