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

  1. Python爬取中国票房网所有电影片名和演员名字,爬取齐鲁网大陆所有电视剧名称

    爬取CBO中国票房网所有电影片名和演员名字 # -*- coding: utf-8 -*- # 爬取CBO中国票房网所有电影片名 import json import requests import ...

  2. python实战项目 — 爬取 校花网图片

    重点: 1.  指定路径创建文件夹,判断是否存在 2. 保存图片文件 # 获得校花网的地址,图片的链接 import re import requests import time import os ...

  3. python实战项目 — 爬取 妹子图网,保存图片到本地

    重点: 1. 用def函数 2. 使用 os.path.dirname("路径保存") , 实现每组图片保存在独立的文件夹中 方法1: import requests from l ...

  4. python爬取中国知网部分论文信息

    爬取指定主题的论文,并以相关度排序. #!/usr/bin/python3 # -*- coding: utf-8 -*- import requests import linecache impor ...

  5. Python爬取中国天气网

    Python爬取中国天气网 基于requests库制作的爬虫. 使用方法:打开终端输入 “python3 weather.py 北京(或你所在的城市)" 程序正常运行需要在同文件夹下加入一个 ...

  6. 初识python 之 爬虫:爬取中国天气网数据

    用到模块: 获取网页并解析:import requests,html5lib from bs4 import BeautifulSoup 使用pyecharts的Bar可视化工具"绘制图表& ...

  7. 利用Python网络爬虫爬取学校官网十条标题

    利用Python网络爬虫爬取学校官网十条标题 案例代码: # __author : "J" # date : 2018-03-06 # 导入需要用到的库文件 import urll ...

  8. python爬取当当网的书籍信息并保存到csv文件

    python爬取当当网的书籍信息并保存到csv文件 依赖的库: requests #用来获取页面内容 BeautifulSoup #opython3不能安装BeautifulSoup,但可以安装Bea ...

  9. python爬虫项目-爬取雪球网金融数据(关注、持续更新)

    (一)python金融数据爬虫项目 爬取目标:雪球网(起始url:https://xueqiu.com/hq#exchange=CN&firstName=1&secondName=1_ ...

随机推荐

  1. 深度学习面试题20:GoogLeNet(Inception V1)

    目录 简介 网络结构 对应代码 网络说明 参考资料 简介 2014年,GoogLeNet和VGG是当年ImageNet挑战赛(ILSVRC14)的双雄,GoogLeNet获得了第一名.VGG获得了第二 ...

  2. centos7使用MariaDB(转)

    转载文章:https://blog.csdn.net/zwkkkk1/article/details/78444581?locationNum=10&fps=1 最近使用centos7,php ...

  3. CNS、ENS和PNS的发育过程

    central nervous system (CNS) peripheral nervous system (PNS) enteric nervous system (ENS) 做这部分的科研必须要 ...

  4. 从内核3.7版本开始,Linux就开始支持VXLAN 到了内核3.12版本,Linux对VXLAN的支持已经完备,支持单播和组播,IPv4和IPv6。

    一.关于VXLAN VXLAN 是 Virtual eXtensible LANs 的缩写,它是对 VLAN 的一个扩展,是非常新的一个 tunnel 技术,在Open vSwitch中应用也非常多. ...

  5. (信贷风控九)行为评分卡模型python实现

    python信用评分卡建模(附代码,博主录制) https://study.163.com/course/introduction.htm?courseId=1005214003&utm_ca ...

  6. Java基础 Scanner 使用nextInt接收整数

        JDK :OpenJDK-11      OS :CentOS 7.6.1810      IDE :Eclipse 2019‑03 typesetting :Markdown   code ...

  7. openresty开发系列27--openresty中封装redis操作

    openresty开发系列27--openresty中封装redis操作 在关于web+lua+openresty开发中,项目中会大量操作redis, 重复创建连接-->数据操作-->关闭 ...

  8. postgresql 利用pgAgent实现定时器任务

    1.安装pgAgent 利用Application Stack Builder安装向导,安装pgAgent. 根据安装向导一步一步安装即可. 安装完成之后,windows服务列表中会增加一个服务:Po ...

  9. 如何发布H5界面可以让公网访问

    本文链接:https://blog.csdn.net/u013310119/article/details/81233560问题背景:手机APP里的H5界面要发布到公网,提供给第三方APP调用. 解决 ...

  10. 【设备问题】罗技M590鼠标无法连接Macbook Pro问题解决

    问题现象 罗技蓝牙鼠标连接的时候一直显示连接中,但是一直连接不上. 解决方法 长按那个切换模式的按钮,重置下,再点击连接,显示能够连接成功.