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. PHP 之快递100接口封装

    <?php /** * Created by PhpStorm. * User: Yang * Date: 2019/8/23 * Time: 10:38 */ class Kuaidi_Que ...

  2. java微服务简介与实战

    今年做了一段时间的可见光.ceph存储,后端开发微服务项目,在这记录点东西,也方便大家借鉴查找. springboot的项目实例:https://github.com/ityouknow/spring ...

  3. XMLHttpRequest Level2 新功能

    XMLHttpRequest是浏览器的接口,使得javascript可以进行HTTP(S)通信: 2008年2月,就提出了XMLHttpRequest Level 2 草案. 这个XMLHttpReq ...

  4. c++自定义数组越异常 ArrayIndexOutOfBoundsException (学习)

    #include <iostream> using namespace std; const int DefaultSize = 10; class Array{public: Array ...

  5. PAT 甲级 1019 General Palindromic Number (进制转换,vector运用,一开始2个测试点没过)

    1019 General Palindromic Number (20 分)   A number that will be the same when it is written forwards ...

  6. Oracle查询序列和函数方法封装

    工具方法: @Autowired private JdbcTemplate businessTpmeJdbcTemplate; @SuppressWarnings("deprecation& ...

  7. 陷门函数Trapdoor Function

    陷门函数:正向计算是很容易的,但若要有效的执行反向计算则必须要知道一些secret/knowledge/trapdoor(知识?),也称为伪随机置换,可用于构造公钥密码系统. 若 f 为陷门函数,则 ...

  8. Node.js使用Express.Router

    在实际开发中通常有几十甚至上百的路由,都写在 index.js 既臃肿又不好维护,这时可以使用 express.Router 实现更优雅的路由解决方案. 目录结构如下: routes的index.js ...

  9. iOS Xib布局某些控件显示或隐藏<约束的修改>

    对于这个问题使用Masonry是很好解决的. 注意:绿色的是label2,当indexpath.section % 2 == 0时,label2不存在. 关键代码如下: if (indexPath.s ...

  10. nginx服务报错解决

    403禁止访问解决 . 重要:修改配置文件使用虚拟机,否则怎么配置都不生效,添加如下用户 [root@host---- html]# ll /etc/nginx/nginx.conf -rw-r--r ...