Python使用BeautifulSoup提取特定HTML标签内容

将网页的HTML元代码down到本地,然后读取

#coding=utf-8
from bs4 import BeautifulSoup
import datetime

def getYesterdayTime():
    now_time=datetime.datetime.now()
    yes_time = now_time + datetime.timedelta(days=-1)
    yesterdaytime = yes_time.strftime('%Y%m%d')
    return yesterdaytime


def getUserNamelist():
    namelist = list()
    htmldoc = open('caijiinfo'+getYesterdayTime()+'.html', 'r').read()
    soup = BeautifulSoup(htmldoc, "html.parser")
    lables = soup.find('div', class_="header").find_all("div")[1]
    for i in range(len(lables)):
        namelist.append('_' + lables[i].find('span', class_='row3').string.encode('utf-8') + '_')
    return namelist


if __name__=='__main__':
    namelist=getUserNamelist()
    print namelist

你可能感兴趣的:(Python)