代码
import requests
from bs4 import BeautifulSoup
def getTitle():`在这里插入代码片`
url = 'https://bbs.ichunqiu.com/portal.php'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36',
}
#构造头不然无法访问
html= requests.get(url,headers=headers)
html=html.text
#进行编码
bs = BeautifulSoup(html,"lxml")
title = bs.find_all("a",class_="ui_colorG",target="blank")
#查找特征
for titles in title:
print (titles.get_text())
#获取标题里的内容
if __name__ == '__main__':
getTitle()
结果: