本文共 1573 字,大约阅读时间需要 5 分钟。
import requestsimport xlwtfrom bs4 import BeautifulSoupdef getItem(): hd = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36 SLBrowser/7.0.0.4071 SLBChan/21"} url = "https://top.baidu.com/board?tab=realtime" page = requests.get(url,headers=hd).content.decode("utf-8") soup = BeautifulSoup(page, 'html.parser') code = [] tBody = soup.find('div',attrs={ 'style':"margin-bottom:20px"}) name_M = tBody.find_all('a',attrs={ 'class':"title_dIF3B"}) text_M = tBody.select("div[class^='hot-desc_1m_jR large_nSuFU']") top_M = tBody.find_all('a', attrs={ 'class': "img-wrapper_29V76"}) score_M = tBody.find_all('div', attrs={ 'class': "hot-index_1Bl1a"}) for i in range(0,len(name_M)): # code.append(dict(排名=top_M[i].div.text,标题=name_M[i].text, # 简介=text_M[i].text,分数=score_M[i].text)) code.append([top_M[i].div.text,name_M[i].text,text_M[i].text,score_M[i].text]) return codedef saveExcel(result): wb = xlwt.Workbook() # 创建工作表 sheet = wb.add_sheet('风云') list_h = ['排名','标题','简介','分数'] for row,str in enumerate(list_h): sheet.write(0, row, str) for i in range(0,len(result)): for n,inV in enumerate(list_h): sheet.write(i+1,n,result[i][inV]) wb.save('热搜爬.xlsx')from openpyxl import Workbookdef OaveExcel(data): workbook = Workbook() sheet = workbook.create_sheet() for x in data: sheet.append(x) workbook.save('热搜爬.xlsx')OaveExcel(getItem())# saveExcel(codex)
转载地址:http://cbuhf.baihongyu.com/