python实战项目 — 爬取中国票房网年度电影信息并保存在csv
import pandas as pd
import requests
from bs4 import BeautifulSoup
import time def spider(url, headers):
print("正在抓取url: " + url)
datas = requests.get(url=url, headers=headers).text
# 解析url
soup = BeautifulSoup(datas, 'lxml')
# 获取数据集合,find_all 返回的是集合类型,所以取[0], 找table标签下 的 属性是 id:tbContent
moives_tables = soup.find_all('table', {'id': 'tbContent'})[0]
# 获取每一个子节点 tr标签
moives = moives_tables.findAll('tr')
# 获取电影名字,电影名字在每个tr标签里面的第一个td标签里面,由于是有多个td所以要用for遍历
names = [tr.find_all('td')[0].a.get('title') for tr in moives[1:]]
# 获取电影的详情页url地址,而且下面提供给获取导演使用,因为导演信息不在主页面上
hrefs = [tr.find_all('td')[0].a.get('href') for tr in moives[1:]]
# 获取电影类型
types = [tr.find_all('td')[1].string for tr in moives[1:]]
# 获取票房数据
box_offices = [int(tr.find_all('td')[2].string) for tr in moives[1:]]
# 获取平均票价
Average_fare = [tr.find_all('td')[3].string for tr in moives[1:]]
# 获取上映日期
show_time = [tr.find_all('td')[6].string for tr in moives[1:]]
# print(names, hrefs, types, box_offices, Average_fare, show_time)
# print(len(hrefs))
daoyans = []
for href in hrefs:
try:
daoyan_datas = requests.get(href)
# 出现错误的原因是因为这里的daoyan_datas是requests对象,无法用BeautifulSoup解析,可以在daoyan_datas后面加上content
soup = BeautifulSoup(daoyan_datas.content, 'lxml')
# 获取导演,由于数据是带换行的,所以要用replace("\n","") 取消换行
daoyan = soup.select('dl.dltext dd')[0].get_text().replace("\n", "")
#print(daoyan)
daoyans.append(daoyan)
#print(len(daoyans))
time.sleep(0.5)
except:
daoyans.append("获取失败")
# 数据拼接,得到的数据类型是 <class 'pandas.core.frame.DataFrame'> ,所以要用 DataFrame() 函数来写入excel
df = pd.DataFrame({
'name': names,
'href': hrefs,
'type': types,
'box_office': box_offices,
'Average_fare': Average_fare,
'show_time': show_time,
'directors': daoyans
})
download(df) '''
问题是不能连续存储,都是重新创建文件csv, os文件操作 mode='a'
'''
def download(df):
df.to_csv('D://box_office.csv', mode='a', index=False, header=False)
print("done") if __name__ == "__main__":
start_time = time.time()
headers = {
'Cookie': 'Hm_lvt_daabace29afa1e8193c0e3000d391562=1570691612; Hm_lpvt_daabace29afa1e8193c0e3000d391562=1570691612',
'Host': 'www.cbooo.cn',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36'
}
base_url = "http://www.cbooo.cn/year?year="
for i in range(2008, 2020):
url = base_url + str(i)
spider(url, headers)
time.sleep(2)
print(round((time.time() - start_time), 3))
python实战项目 — 爬取中国票房网年度电影信息并保存在csv的更多相关文章
- Python爬取中国票房网所有电影片名和演员名字,爬取齐鲁网大陆所有电视剧名称
爬取CBO中国票房网所有电影片名和演员名字 # -*- coding: utf-8 -*- # 爬取CBO中国票房网所有电影片名 import json import requests import ...
- python实战项目 — 爬取 校花网图片
重点: 1. 指定路径创建文件夹,判断是否存在 2. 保存图片文件 # 获得校花网的地址,图片的链接 import re import requests import time import os ...
- python实战项目 — 爬取 妹子图网,保存图片到本地
重点: 1. 用def函数 2. 使用 os.path.dirname("路径保存") , 实现每组图片保存在独立的文件夹中 方法1: import requests from l ...
- python爬取中国知网部分论文信息
爬取指定主题的论文,并以相关度排序. #!/usr/bin/python3 # -*- coding: utf-8 -*- import requests import linecache impor ...
- Python爬取中国天气网
Python爬取中国天气网 基于requests库制作的爬虫. 使用方法:打开终端输入 “python3 weather.py 北京(或你所在的城市)" 程序正常运行需要在同文件夹下加入一个 ...
- 初识python 之 爬虫:爬取中国天气网数据
用到模块: 获取网页并解析:import requests,html5lib from bs4 import BeautifulSoup 使用pyecharts的Bar可视化工具"绘制图表& ...
- 利用Python网络爬虫爬取学校官网十条标题
利用Python网络爬虫爬取学校官网十条标题 案例代码: # __author : "J" # date : 2018-03-06 # 导入需要用到的库文件 import urll ...
- python爬取当当网的书籍信息并保存到csv文件
python爬取当当网的书籍信息并保存到csv文件 依赖的库: requests #用来获取页面内容 BeautifulSoup #opython3不能安装BeautifulSoup,但可以安装Bea ...
- python爬虫项目-爬取雪球网金融数据(关注、持续更新)
(一)python金融数据爬虫项目 爬取目标:雪球网(起始url:https://xueqiu.com/hq#exchange=CN&firstName=1&secondName=1_ ...
随机推荐
- Unity设计模式+Java设计模式,讲解+案例+PPT,一次性学会设计模式,拥抱高薪!
一个程序员对设计模式的理解:“不懂”为什么要把很简单的东西搞得那么复杂.后来随着软件开发经验的增加才开始明白我所看到的“复杂”恰恰就是设计模式的精髓所在,我所理解的“简单”就是一把钥匙开一把锁的模式, ...
- 《Linux设备驱动程序》第三版 scull编译 Ubuntu18.04
0 准备工作. 0.0 系统环境:Ubuntu18.04.1 amd64. 0.1 安装必要软件包 1 sudo apt install build-essential bison flex libs ...
- oracle取前10条记录
--oracle取前十条数据 --(1)第一种 ; --(2)第二种 ;
- 如何选题?| 什么样的科学问题 | 研究项目才是有意义的?| scientific method
搞科研,尤其是生命科学,经常会觉得自己做的东西是坨屎,没有任何意义. 在硕博的时候这种感觉会非常强烈,一个是自己思考能力不足:二是你的项目不是你设计的,不懂个中缘由,只执行的话就会很无聊,找不到意义感 ...
- GPS 经纬度
经纬度地图: http://www.gpsspg.com/maps.htm http://www.gzhatu.com/dingwei.html 经纬度格式转化 http://www.gzhatu ...
- face morhper
图像变形背后的想法很简单.给定两个图像,我们想通过将图像和混合来创建中间图像.图像的混合和由参数控制的是在0和1之间().当为0时,变形看起来像,而当为1 时,变形看起来像.天真的,您可以在每个像素上 ...
- Java之Object对象中的wait()和notifyAll()用法
用一个例子来说明Object对象中的wait方法和notifyAll方法的使用. 首先定义一个消息类,用于封装数据,以供读写线程进行操作: /** * 消息 * * @author syj */ pu ...
- Linux 在 TOP 命令中切换内存的显示单位
顶部的内存信息可以在top运行时按E切换,每次切换转换率为1000,只是没有单位,切换的单位为 k,m,g,t,p: 1. 2. 3., 4. 底下的进程信息按e切换,每次切换转换率为1000,切换的 ...
- SeetaFaceDetection识别人脸
SeetaFaceDetection识别人脸 #pragma warning(disable: 4819) #include <seeta/FaceEngine.h> #include & ...
- 【433】COMP9024 复习
目录: 01. Week01 - Lec02 - Revision and setting the scene 02. Week02 - Lec01 - Data structures - memor ...