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的更多相关文章

  1. Python爬取豆瓣音乐存储MongoDB数据库(Python爬虫实战1)

    1.  爬虫设计的技术 1)数据获取,通过http获取网站的数据,如urllib,urllib2,requests等模块: 2)数据提取,将web站点所获取的数据进行处理,获取所需要的数据,常使用的技 ...

  2. python 爬取文章

    这里我们利用强大的python爬虫来爬取一篇文章.仅仅做一个示范,更高级的用法还要大家自己实践. 好了,这里就不啰嗦了,找到一篇文章的url地址:http://www.duanwenxue.com/a ...

  3. python爬取数据保存到Excel中

    # -*- conding:utf-8 -*- # 1.两页的内容 # 2.抓取每页title和URL # 3.根据title创建文件,发送URL请求,提取数据 import requests fro ...

  4. Python爬取新浪微博评论数据,写入csv文件中

    因为新浪微博网页版爬虫比较困难,故采取用手机网页端爬取的方式 操作步骤如下: 1. 网页版登陆新浪微博 2.打开m.weibo.cn 3.查找自己感兴趣的话题,获取对应的数据接口链接 4.获取cook ...

  5. 用Python爬取文章,并转PDF格式电子书

    wkhtmltopdf [软件],这个是必学准备好的,不然这个案例是实现不出来的 获取文章内容代码 (https://jq.qq.com/?_wv=1027&k=QgGWqAVF) 发送请求, ...

  6. python爬取旅游数据+matplotlib简单可视化

    题目如下: 共由6个函数组成: 第一个函数爬取数据并转为DataFrame: 第二个函数爬取数据后存入Excel中,对于解题来说是多余的,仅当练手以及方便核对数据: 后面四个函数分别对应题目中的四个m ...

  7. Python爬虫教程-13-爬虫使用cookie爬取登录后的页面(人人网)(下)

    Python爬虫教程-13-爬虫使用cookie爬取登录后的页面(下) 自动使用cookie的方法,告别手动拷贝cookie http模块包含一些关于cookie的模块,通过他们我们可以自动的使用co ...

  8. Python爬取招聘信息,并且存储到MySQL数据库中

    前面一篇文章主要讲述,如何通过Python爬取招聘信息,且爬取的日期为前一天的,同时将爬取的内容保存到数据库中:这篇文章主要讲述如何将python文件压缩成exe可执行文件,供后面的操作. 这系列文章 ...

  9. Python爬虫教程-12-爬虫使用cookie爬取登录后的页面(人人网)(上)

    Python爬虫教程-12-爬虫使用cookie(上) 爬虫关于cookie和session,由于http协议无记忆性,比如说登录淘宝网站的浏览记录,下次打开是不能直接记忆下来的,后来就有了cooki ...

随机推荐

  1. CF1197B

    CF1197B 题意: 出n个柱子,每个柱子一个圆盘,其半径各不相同,每次只能将柱子上只有一个圆盘的移到相邻位置,问能否全部移到一个柱子上. 解法: 思路题. 如果所有盘子都能移动到同一个柱子上,那么 ...

  2. 算法-java实现

    1. 质因数分解 public static List<Integer> factorize(int n){ List<Integer> factors = new Array ...

  3. java跨域配置

    一.问题 使用前后端分离模式开发项目时,往往会遇到这样一个问题 -- 无法跨域获取服务端数据 这是由于浏览器的同源策略导致的,目的是为了安全.在前后端分离开发模式备受青睐的今天,前端和后台项目往往会在 ...

  4. HearthBuddy炉石兄弟 Method 'Entity.GetRace' not found.

    解决方案 namespace Triton.Game.Mapping{// Token: 0x020004A4 RID: 1188[Attribute38("Entity")]pu ...

  5. JS模拟Touch事件

    var ele = document.getElementsByClassName('target_node_class')[0] //may have x and y properties in s ...

  6. linux下的什么工具能将DVI文件转换成PostScript文件?

    答: dvips,此工具能将由Latex或Tex生成的DVI文件转换成PostScript文件,官网在此

  7. C++ STL 逆转旋转 reverse reverse_copy rotate

    #include <iostream>#include <algorithm>#include <vector>#include <iterator> ...

  8. VituralBox从零搭建基于CentOS 7(64位)的Kubernetes+docker集群

    1. 下载CentOS 7官方minimal镜像 2. 安装VituralBox(Windows 10 64位) 3. 安装Git for windows(Windows 10 64位) 4. 安装V ...

  9. 阶段5 3.微服务项目【学成在线】_day04 页面静态化_03-freemarker测试环境搭建

    新建一个module 选择parent test-freemarker spring‐boot‐starter‐freemarker:spring boot 提供的关于 freemaker的相关的包 ...

  10. React Native使用NetInfo对当前系统网络的判断

    有网状态: 断网状态: 代码如下: 注意:第一次参考了http://www.hangge.com/blog/cache/detail_1614.html代码,一直显示的是unknow状态... 最后处 ...