python 爬取文章后存储excel 以及csv
import requests
from bs4 import BeautifulSoup
import random
import openpyxl
xls=openpyxl.Workbook()
sheet=xls.active
sheet.title='movies'
sheet['A1']='序号'
sheet['B1']='名称'
sheet['C1']='评分'
sheet['D1']='推荐语'
sheet['E1']='链接' for i in range(11):
params={
'start': str(i*25),
'filter':''
}
headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36'
}
url='https://movie.douban.com/top250'
res=requests.get(url,params=params,headers=headers)
con=res.text
soup=BeautifulSoup(con,'html.parser')
maindiv=soup.find(class_="grid_view")
for titles in maindiv.find_all('li'):
try:
num = titles.find('em',class_="").text
#查找序号
title = titles.find('span', class_="title").text
#查找电影名
tes = titles.find('span',class_="inq").text
#查找推荐语
comment = titles.find('span',class_="rating_num").text
#查找评分
url_movie = titles.find('a')['href']
print(num + '.' + title + '——' + comment + '\n' + '推荐语:' + tes +'\n' + url_movie)
sheet.append([num,title,comment,tes,url_movie])
except:
continue
xls.save('douban.xlsx')
csv:
import requests
from bs4 import BeautifulSoup
import random
import openpyxl
import csv url="https://www.zhihu.com/api/v4/members/zhang-jia-wei/articles"
headers={
'referer': 'https://www.zhihu.com/people/zhang-jia-wei/posts/posts_by_votes?page=1',
'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'
}
csv_file=open('dazhangwei.csv','w',newline='',encoding='utf-8')
writer=csv.writer(csv_file)
header=['标题','简介','连接']
writer.writerow(header)
x=0
while True:
params={
'include': 'data[*].comment_count,suggest_edit,is_normal,thumbnail_extra_info,thumbnail,can_comment,comment_permission,admin_closed_comment,content,voteup_count,created,updated,upvoted_followees,voting,review_info,is_labeled,label_info;data[*].author.badge[?(type=best_answerer)].topics',
'offset': str((x*10)),
'limit': '10',
'sort_by': 'voteups'
}
res=requests.get(url,headers=headers,params=params)
res_json=res.json()
con=res_json['data']
for i in con:
lists=[i['title'],i['url'],i['excerpt']]
writer.writerow(lists)
if res_json['paging']['is_end'] == True:
break
x+=1
csv_file.close()
python 爬取文章后存储excel 以及csv的更多相关文章
- Python爬取豆瓣音乐存储MongoDB数据库(Python爬虫实战1)
1. 爬虫设计的技术 1)数据获取,通过http获取网站的数据,如urllib,urllib2,requests等模块: 2)数据提取,将web站点所获取的数据进行处理,获取所需要的数据,常使用的技 ...
- python 爬取文章
这里我们利用强大的python爬虫来爬取一篇文章.仅仅做一个示范,更高级的用法还要大家自己实践. 好了,这里就不啰嗦了,找到一篇文章的url地址:http://www.duanwenxue.com/a ...
- python爬取数据保存到Excel中
# -*- conding:utf-8 -*- # 1.两页的内容 # 2.抓取每页title和URL # 3.根据title创建文件,发送URL请求,提取数据 import requests fro ...
- Python爬取新浪微博评论数据,写入csv文件中
因为新浪微博网页版爬虫比较困难,故采取用手机网页端爬取的方式 操作步骤如下: 1. 网页版登陆新浪微博 2.打开m.weibo.cn 3.查找自己感兴趣的话题,获取对应的数据接口链接 4.获取cook ...
- 用Python爬取文章,并转PDF格式电子书
wkhtmltopdf [软件],这个是必学准备好的,不然这个案例是实现不出来的 获取文章内容代码 (https://jq.qq.com/?_wv=1027&k=QgGWqAVF) 发送请求, ...
- python爬取旅游数据+matplotlib简单可视化
题目如下: 共由6个函数组成: 第一个函数爬取数据并转为DataFrame: 第二个函数爬取数据后存入Excel中,对于解题来说是多余的,仅当练手以及方便核对数据: 后面四个函数分别对应题目中的四个m ...
- Python爬虫教程-13-爬虫使用cookie爬取登录后的页面(人人网)(下)
Python爬虫教程-13-爬虫使用cookie爬取登录后的页面(下) 自动使用cookie的方法,告别手动拷贝cookie http模块包含一些关于cookie的模块,通过他们我们可以自动的使用co ...
- Python爬取招聘信息,并且存储到MySQL数据库中
前面一篇文章主要讲述,如何通过Python爬取招聘信息,且爬取的日期为前一天的,同时将爬取的内容保存到数据库中:这篇文章主要讲述如何将python文件压缩成exe可执行文件,供后面的操作. 这系列文章 ...
- Python爬虫教程-12-爬虫使用cookie爬取登录后的页面(人人网)(上)
Python爬虫教程-12-爬虫使用cookie(上) 爬虫关于cookie和session,由于http协议无记忆性,比如说登录淘宝网站的浏览记录,下次打开是不能直接记忆下来的,后来就有了cooki ...
随机推荐
- Kafka - SASL认证
kafka SASL认证配置 1.找到kafka安装根目录,在config文件夹下创建kafka_server_jaas.conf,写入 KafkaServer { org.apache.kafka. ...
- MySQL个人用户的安装配置详解
1. 我的版本是 MySQL 5.7.26.0 ,因为据说 MySQL 8 的性能虽然强悍,但是兼容性还是有问题,而且发布时间不够长,没有普及,就暂时用着5.7版本. (1) 下载地址,选择使用msi ...
- 冲刺阶段——Day5
[今日进展] 完成注册功能代码 import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionLi ...
- spring boot jpa-java.lang.IllegalArgumentException: Not a managed type异常问题解决方法
JPA实体类没有被扫描到,导致这样的情况有以下几种可能: 实体类没有加上@Entity注解 对应解决方法在实体类上加上@Entity即可解决问题 没有按照SpringBoot的约定,默认扫描(appl ...
- asp.net core spa应用(angular) 部署同一网站下
需求:现在一个应用是前后端开发分离,前端使用angular,后端使用 asp.net core 提供api ,开发完成后,现在需要把两个程序部署在同一个网站下,应该怎么处理? 首先可以参考微软的官方文 ...
- vue单页面项目架构方案
这里的架构方案是基于vue-cli2生成的项目应用程序产生的,是对项目应用程序或者项目模板的一些方便开发和维护的封装.针对单页面的解决方案. 主要有四个方面: 一,不同环境下的分别打包 主要是测试环境 ...
- MySQL表的创建与维护
一.导入测试数据 [root@server ~]# wget https://launchpadlibrarian.net/24493586/employees_db-full-1.0.6.tar.b ...
- PAT 甲级 1023 Have Fun with Numbers (20 分)(permutation是全排列题目没读懂)
1023 Have Fun with Numbers (20 分) Notice that the number 123456789 is a 9-digit number consisting ...
- Python基础-day04
函数基础 目标 函数的快速体验 函数的基本使用 函数的参数 函数的返回值 函数的嵌套调用 在模块中定义函数 01. 函数的快速体验 1.1 快速体验 所谓函数,就是把 具有独立功能的代码块 组织为一个 ...
- swift 第十三课 GCD 的介绍和使用
手头的项目中基本没有用到这个,但是还是要探索下.毕竟好多的地方要用这个,而且现在手机和电脑等电子设备都是多核心的,这样就成就了多线程带来更加优越的用户体验. 先记录下,自己看到的两个不错的连接: ht ...