无聊拿来玩儿,仅作为练习用。
分析
找到文本的所处的div即可,注意应该是文本(一般是<span>text</span>
这种形式)的上一级div,即div下面应该就是文本所在的span,找到文本所在的tag之后,使用.get_text()获取其文本信息。
代码
import requests
from bs4 import BeautifulSoup
# 获取html文档
def get_html(url):
"""get the content of the url"""
response = requests.get(url)
response.encoding = 'utf-8' #中文乱码的话,可以试一下gb2312
return response.text
# 获取内容
def get_certain_web(html):
"""get the content of the html"""
global soup #方便调试
soup = BeautifulSoup(html, 'lxml') #使用lxml解析器对html进行解析,生成soup结构化文件
web_content_temp = soup.select('div.index-tab-notice-right-list-title')#根据所需内容进行筛选
web_content = ''
for i in web_content_temp:
web_content += i.get_text() + '\n'
#web_content = soup.find('div',{'class':'zzj_5b_2d'})[0].get_text() 也可以使用这个
return web_content
url_web = "http://www.zzu.edu.cn/"
html = get_html(url_web)
web_content = get_certain_web(html)
print(web_content)