import requests,os,json
from lxml import etree
from pymysql import *
class Movie(object):
def __init__(self):
self.url="https://www.360kan.com/dianying/list.php?year=all&area=all&act=all&cat=106&pageno={}"
self.headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36'}
self.start=0
# 请求数据接受数据
def get_data(self,url):
response = requests.get(url,headers=self.headers)
return response.content
# 解析数据
def xml_data(self,data):
html = etree.HTML(data)
mes = html.xpath("//ul[@class='list g-clear']/*")
dict = {}
for i in mes:
dict['title'] = i.xpath("./a[@class='js-tongjic']/div[@class='detail']/p/span/text()")[0]
dict['zhuyan'] = i.xpath("./a[@class='js-tongjic']/div[@class='detail']/p[last()]/text()")[0]
info_url = "https://www.360kan.com" + i.xpath("./a[@class='js-tongjic']/@href")[0] poto_url = i.xpath("./a[@class='js-tongjic']/div[@class='cover g-playicon']/img/@src")
potoname =self.write_poto(poto_url)
dict['poto'] = potoname
mes = self.get_data(info_url)
meslist = etree.HTML(mes)
mes_info = meslist.xpath("//div[@class='top-info-detail g-clear']")
dictid = self.write_database(dict)
for w in mes_info:
dict1 = {}
dict1['daoyan'] = w.xpath("./div[@class='g-clear item-wrap']/p[5]/a/text()")[0]
dict1['diqu'] = w.xpath("./div[@class='g-clear item-wrap']/p[3]/text()")[0]
dict1['year'] = w.xpath("./div[@class='g-clear item-wrap']/p[2]/text()")[0]
dict1['info'] = w.xpath("./div[@class='item-desc-wrap g-clear js-open-wrap']/p/text()")[0]
dict1['t_id'] = str(dictid)
self.info_write_database(dict1)
# 存图片
def write_poto(self,data):
path = "d:/img/"
if not os.path.exists(path):
os.makedirs(path)
for n in data:
poto_mes = self.get_data(n)
potoname = n.split("/")[-1]
with open(path+potoname,"wb") as f:
f.write(poto_mes)
return potoname
# 写入数据库
def database(self):
conn = connect(host="127.0.0.1",port=3306,user="root",password="mysql",database="lianxi",charset="utf8mb4")
cur = conn.cursor()
return cur,conn
# 写入数据库
def write_database(self,data):
cur,conn = self.database()
title = data["title"]
zhuyan = data["zhuyan"]
poto = data["poto"]
try:
cur.execute("insert into movie values(0,'"+title+"','"+zhuyan+"','"+poto+"')")
except:
pass
new_id = cur.lastrowid
conn.commit() return new_id # 读取数据库
def read_database(self,):
cur,conn = self.database()
cur.execute("select * from movie inner join movies on movie.id=movies.t_id limit 10;")
ret =cur.fetchall()
cur.close()
conn.close()
list = json.dumps(ret,ensure_ascii=False)
print(list)
# info写入数据库
def info_write_database(self,data1):
cur,conn = self.database()
cur.execute("insert into movies values(0,'"+data1['daoyan']+"','"+data1['diqu']+"','"+data1['year']+"','"+data1['info']+"','"+data1['t_id']+"')")
conn.commit() def run(self):
while True:
url = self.url.format(self.start)
data = self.get_data(url)
if data ==[]:
break
else:
if self.start == 2:
break
else:
self.start+=1
self.xml_data(data) if __name__ == '__main__':
movie = Movie()
movie.run()
movie.read_database()

360电影主页和详情页爬去入Mysql库链表读取--lowbiprogrammer的更多相关文章

  1. bokeyuan_python文章爬去入mongodb读取--LOWBIPROGRAMMER

    # -*- coding: utf-8 -*- import requests,os from lxml import etree from pymongo import * class Boke(o ...

  2. java批量爬去电影资源

    摘要 网上有很多个人站来分享电影资源,其实有时候我们自己也想做这个一个电影站来分享资源.但是这个时候就有一个问题,电影的资源应该从哪里来呢?难道要自己一条条手动去从网络上获取,这样无疑是缓慢而又效率低 ...

  3. Android 自定义控件 轻松实现360软件详情页

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/43649913,本文出自:[张鸿洋的博客] 1.概述 最近有不少朋友私聊问应用宝. ...

  4. 多线程爬虫爬取详情页HTML

    注意:如果想爬取详情页的信息请按须添加方法 import requests import os import re import threading from lxml import etree #爬 ...

  5. 1、找出url汇总页,过滤出满足条件的详情页url;2、去详情页采集信息

    1.找出url汇总页,过滤出满足条件的详情页url:2.去详情页采集信息 package main import ( "fmt" "github.com/gocolly/ ...

  6. 潭州课堂25班:Ph201805201 django 项目 第二十三课 文章主页 轮播图前端实现 热门新闻推荐实现 详情页实现 (课堂笔记)

    前台代码 // 在static/js/news/index.js文件中 $(function () { // 新闻列表功能 let $newsLi = $(".news-nav ul li& ...

  7. scrapy爬取网址,进而爬取详情页问题

    1.最容易出现的问题是爬取到的url大多为相对路径,如果直接将爬取到的url进行二次爬取就会出现以下报错: raise ValueError('Missing scheme in request ur ...

  8. 【京东详情页】——原生js爬坑之放大镜

    一.引言 在商城的详情页中,放大镜的功能是很常见的.这里京东详情页就要做一个仿放大镜的效果,预览如下: 二.实现原理 实际上,放大镜的实现是单纯用几个div,鼠标移入其中一个小图div,触发事件显示另 ...

  9. 【京东详情页】——原生js爬坑之标签页

    一.引言 要做详情页的商品评价等5个li的标签页转换,效果如下: 二.实现原理 有一个特别的地方:上面五个li,但下面只有四个容器(table/div). 设计的目的:无论点哪个li,只有前四个div ...

随机推荐

  1. str.split()与re.split()的区别

    str.split(): >>>'hello, world'.split() >>>['hello,','world'] >>>'hello, w ...

  2. Android开发-- 简单对话框

    final Builder builder = new AlertDialog.Builder(this); builder.setIcon(R.drawable.appicns_folder_sma ...

  3. Splash go() 方法

    go()方法用来请求某个链接,而且它可以模拟 GET 和 POST 请求,同时支持传入请求头.表单等数据 function main(splash) ok, reason = splash:go(&q ...

  4. Bugly最简单的配置方法

    1,注册Bugly帐号 2.项目 build.gradle 中配置 compile 'com.tencent.bugly:crashreport:latest.release'//其中latest.r ...

  5. 使用createprocess()创建进程打开其他文件方法

    #include "stdafx.h"#include "windows.h"#include <iostream>#include "s ...

  6. Swift - 类型转换(as as! as?)

    swift 类型转换 一,as 1,as使用场合 (1)从派生类转换为基类,向上转型(upcasts) class Animal {} class Cat: Animal {} let cat = C ...

  7. 原:Myeclipse10+Egit+bitbucket实现版本控制

    1.首先在https://bitbucket.org注册账号,建立仓库(repository),这部分有问题的可以看https://confluence.atlassian.com/display/B ...

  8. 大智慧F10离线资料压缩包带F10关键字过滤软件--更新于2014-01-06.rar 184MB

    大智慧F10离线资料包带F10关键字过滤软件--更新于2014-01-06.rar 移步到百度网盘下载: http://pan.baidu.com/s/1c01PDnE

  9. C++中三种创建对象的方法【转】

    我们都知道C++中有三种创建对象的方法,如下: #include <iostream> using namespace std; class A { private: int n; pub ...

  10. Qt编写网络调试助手(TCP客户端+TCP服务端+UDP服务端)终极版开源

    时隔半年,对网络调试助手工具进行所有代码重写,这次目录结果整齐的一逼,代码整齐的一逼,非常完善了,打死也不再改版了.这次真的打死也不再改版了.旧版本1:http://www.qtcn.org/bbs/ ...