import requests
from bs4 import BeautifulSoup
def get_article_info(url):
# 获取整个页面 html 内容
r = requests.get(url)
html = r.text
soup = BeautifulSoup(html)
# 获取页面标题
title = soup.title.string
# 获取左侧菜单 html 内容
menu_html = soup.find('ul', class_='list-group list-group-flush')
# 从左侧菜单 html 内容中获取所有菜单信息
menu_info = [(a['href'], a.string) for a in menu_html.find_all('a')]
return title, menu_info
if __name__ == '__main__':
url = 'https://python-abc.xyz/basic/5642/'
title, menu_info = get_article_info(url)
print('文章标题为:', title)
print('菜单列表如下:')
for menu in menu_info:
print('链接:https://python-abc.xyz{};标题:{}'.format(*menu))