生成一段代码,用python编写,能够爬取几个不同网页上,每条新闻的标题,作者,时间,最终导入excel表里
时间: 2023-03-12 17:09:29 浏览: 96
import requests
from bs4 import BeautifulSoup
import xlwt# 创建excel表
workbook = xlwt.Workbook(encoding = 'utf-8')
sheet = workbook.add_sheet('Sheet1') # 设置表头
row0 = ['标题','作者','时间'] # 向表中写入表头
for i in range(0,3):
sheet.write(0,i,row0[i]) # 爬取数据
urls = ['http://example1.com','http://example2.com','http://www.example3.com']
row_num = 1
for url in urls:
response = requests.get(url)
soup = BeautifulSoup(response.text,'html.parser')
titles = soup.find_all('div',class_='title')
authors = soup.find_all('div',class_='author')
times = soup.find_all('div',class_='time')
for i in range(0,len(titles)):
sheet.write(row_num,0,titles[i].string)
sheet.write(row_num,1,authors[i].string)
sheet.write(row_num,2,times[i].string)
row_num += 1# 保存excel表
workbook.save('news.xls')
阅读全文