爬取豆瓣电影top250并存储到mysql数据库
import requests
from lxml import etree
import re
import pymysql
import time conn= pymysql.connect(host='localhost',user='root',passwd='root',db='mydb',port=3306,charset='utf8')
cursor=conn.cursor() headers={
#'User-Agent':'Nokia6600/1.0 (3.42.1) SymbianOS/7.0s Series60/2.0 Profile/MIDP-2.0 Configuration/CLDC-1.0'
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'
} def get_movie_url(url):
html=requests.get(url,headers=headers)
selector=etree.HTML(html.text)
movie_hrefs=selector.xpath('//div[@class="hd"]/a/@href')
for movie_href in movie_hrefs:
get_movie_info(movie_href)
time.sleep(2) def get_movie_info(url):
html=requests.get(url,headers=headers)
print("开始")
selector=etree.HTML(html.text)
try:
name=selector.xpath('//*[@id="content"]/h1/span[1]/text()')[0]
director=selector.xpath('//*[@id="info"]/span[1]/span[2]/a/text()')[0]
actors=selector.xpath('//*[@id="info"]/span[3]/span[2]')[0]
actor=actors.xpath('string(.)')
#style=selector.xpath('//*[@id="info"]/span[5]/text()')[0]
style=re.findall('<span property="v:genre">(.*?)</span>',html.text,re.S) #爬取制片国家不成功
#country=re.findall('<span class="pl">制片国家/地区:</span>(.*?)<b',selector.text,re.S)[0]
#country=re.findall('<span property="v:genre">.*?地区:</span>(.*?)<b',selector.text,re.S)[0]
#countrys=selector.xpath('//*[@id="info"]/br[4]')[0]
#country=selector.xpath('//*[@id="info"]/text()[2]')[0] release_time=re.findall('上映日期:</span>.*?>(.*?)</span>',html.text,re.S)[0]
time=re.findall('片长:</span>.*?>(.*?)</span>',html.text,re.S)[0]
score=selector.xpath('//*[@id="interest_sectl"]/div[1]/div[2]/strong/text()')[0]
cursor.execute("insert into doubanmovie (name,director,actor,style,release_time,time,score)values(%s,%s,%s,%s,%s,%s,%s)",(str(name),str(director),str(actor),str(style),str(release_time),str(time),str(score))) except IndexError:
pass if __name__=='__main__':
urls=['https://movie.douban.com/top250?start={}'.format(str(i)) for i in range(0,25,25)]
for url in urls:
get_movie_url(url)
time.sleep(2)
conn.commit()
print("结束")

爬取豆瓣电影top250并存储到mysql数据库的更多相关文章
- python2.7爬取豆瓣电影top250并写入到TXT,Excel,MySQL数据库
python2.7爬取豆瓣电影top250并分别写入到TXT,Excel,MySQL数据库 1.任务 爬取豆瓣电影top250 以txt文件保存 以Excel文档保存 将数据录入数据库 2.分析 电影 ...
- urllib+BeautifulSoup无登录模式爬取豆瓣电影Top250
对于简单的爬虫任务,尤其对于初学者,urllib+BeautifulSoup足以满足大部分的任务. 1.urllib是Python3自带的库,不需要安装,但是BeautifulSoup却是需要安装的. ...
- scrapy爬虫框架教程(二)-- 爬取豆瓣电影TOP250
scrapy爬虫框架教程(二)-- 爬取豆瓣电影TOP250 前言 经过上一篇教程我们已经大致了解了Scrapy的基本情况,并写了一个简单的小demo.这次我会以爬取豆瓣电影TOP250为例进一步为大 ...
- scrapy爬取豆瓣电影top250
# -*- coding: utf-8 -*- # scrapy爬取豆瓣电影top250 import scrapy from douban.items import DoubanItem class ...
- 一起学爬虫——通过爬取豆瓣电影top250学习requests库的使用
学习一门技术最快的方式是做项目,在做项目的过程中对相关的技术查漏补缺. 本文通过爬取豆瓣top250电影学习python requests的使用. 1.准备工作 在pycharm中安装request库 ...
- python 爬虫&爬取豆瓣电影top250
爬取豆瓣电影top250from urllib.request import * #导入所有的request,urllib相当于一个文件夹,用到它里面的方法requestfrom lxml impor ...
- 【转】爬取豆瓣电影top250提取电影分类进行数据分析
一.爬取网页,获取需要内容 我们今天要爬取的是豆瓣电影top250页面如下所示: 我们需要的是里面的电影分类,通过查看源代码观察可以分析出我们需要的东西.直接进入主题吧! 知道我们需要的内容在哪里了, ...
- Scrapy中用xpath/css爬取豆瓣电影Top250:解决403HTTP status code is not handled or not allowed
好吧,我又开始折腾豆瓣电影top250了,只是想试试各种方法,看看哪一种的方法效率是最好的,一直进行到这一步才知道 scrapy的强大,尤其是和selector结合之后,速度飞起.... 下面我就采用 ...
- Python爬虫入门:爬取豆瓣电影TOP250
一个很简单的爬虫. 从这里学习的,解释的挺好的:https://xlzd.me/2015/12/16/python-crawler-03 分享写这个代码用到了的学习的链接: BeautifulSoup ...
随机推荐
- 数组,for语句(补10.11)
1.数组定义:一系列通数据类型的数据集合. 2.数组赋值的两种方法: 先定义后赋值:(赋值从0开始) var aa = new Arrey(); aa[0] = 1; aa[1] = 2; 定义并赋值 ...
- 实现简易版shell
操作系统小组作业,实现一个简易shell,shell实现了下列命令 exit------退出终端命令 clr-------清屏命令 time-----时间命令 myshell----欢迎命令 quit ...
- tf.pad()
说一下我理解的tf.pad(),先来看一下定义: def pad(tensor, paddings, mode="CONSTANT", name=None, constant_ ...
- css border实现三角形
实现过程: 正常的border <div class="box"></div> .box { background: #ddd; width: 100px; ...
- ue4 2游戏构架相关
官网:游戏性指南 https://docs.unrealengine.com/latest/CHN/Gameplay/index.html 深入UE4剖析源码,浅出GameEngine设计理念,比较好 ...
- Litjson
http://www.cnblogs.com/peiandsky/archive/2012/04/20/2459219.html JSON(JavaScript Object Notation) 是一 ...
- php网页上一页下一页翻页
这几天做项目因为数据太多,需要对信息进行上下翻页展示,就自己写了翻页的代码 大致功能就是页面只显示几条信息,按上一页.下一页切换内容,当显示第一页时上一页和首页选项不可选,当页面加载到最后一页时下一页 ...
- centos 创建 logrotate 进行日志分割
这里就不赘述logrotate了,具体是什么,有什么作用,自行百度. 我们先说下,如何进行nginx的日志切割: 比如:日志目录为:/usr/local/nginx/logs/access.log按照 ...
- jmeter的Include Controller控件和Test Fragment控件和Module Controller控件
1.Include Controller控件——给当前jmeter脚本引入外部片段的jmeter脚本(Test Fragment片段脚本) Include Controller控件——添加外部jmet ...
- Codeforces Round #432 (Div. 2, based on IndiaHacks Final Round 2017) D
Arpa has found a list containing n numbers. He calls a list bad if and only if it is not empty and g ...