360电影主页和详情页爬去入Mysql库链表读取--lowbiprogrammer
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的更多相关文章
- bokeyuan_python文章爬去入mongodb读取--LOWBIPROGRAMMER
# -*- coding: utf-8 -*- import requests,os from lxml import etree from pymongo import * class Boke(o ...
- java批量爬去电影资源
摘要 网上有很多个人站来分享电影资源,其实有时候我们自己也想做这个一个电影站来分享资源.但是这个时候就有一个问题,电影的资源应该从哪里来呢?难道要自己一条条手动去从网络上获取,这样无疑是缓慢而又效率低 ...
- Android 自定义控件 轻松实现360软件详情页
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/43649913,本文出自:[张鸿洋的博客] 1.概述 最近有不少朋友私聊问应用宝. ...
- 多线程爬虫爬取详情页HTML
注意:如果想爬取详情页的信息请按须添加方法 import requests import os import re import threading from lxml import etree #爬 ...
- 1、找出url汇总页,过滤出满足条件的详情页url;2、去详情页采集信息
1.找出url汇总页,过滤出满足条件的详情页url:2.去详情页采集信息 package main import ( "fmt" "github.com/gocolly/ ...
- 潭州课堂25班:Ph201805201 django 项目 第二十三课 文章主页 轮播图前端实现 热门新闻推荐实现 详情页实现 (课堂笔记)
前台代码 // 在static/js/news/index.js文件中 $(function () { // 新闻列表功能 let $newsLi = $(".news-nav ul li& ...
- scrapy爬取网址,进而爬取详情页问题
1.最容易出现的问题是爬取到的url大多为相对路径,如果直接将爬取到的url进行二次爬取就会出现以下报错: raise ValueError('Missing scheme in request ur ...
- 【京东详情页】——原生js爬坑之放大镜
一.引言 在商城的详情页中,放大镜的功能是很常见的.这里京东详情页就要做一个仿放大镜的效果,预览如下: 二.实现原理 实际上,放大镜的实现是单纯用几个div,鼠标移入其中一个小图div,触发事件显示另 ...
- 【京东详情页】——原生js爬坑之标签页
一.引言 要做详情页的商品评价等5个li的标签页转换,效果如下: 二.实现原理 有一个特别的地方:上面五个li,但下面只有四个容器(table/div). 设计的目的:无论点哪个li,只有前四个div ...
随机推荐
- str.split()与re.split()的区别
str.split(): >>>'hello, world'.split() >>>['hello,','world'] >>>'hello, w ...
- Ansible 实战:一键安装 LNMP
Ansible 配置文件 : [root@center /data/ansiblework]# cat ansible.cfg [defaults] remote_user = root remote ...
- Splash jsfunc() 方法
jsfunc()方法可以直接调用 JavaScript 定义的方法,但是所调用的方法需要用双中括号包围,这相当于实现了 JavaScript 方法到 Lua 脚本的转换 function main(s ...
- 微信小程序的wx-charts插件
还有就是可以使用一些小程序的插件,比如wx-charts. 先来看一下网上对这个插件的评价: 目前在github上有1804颗星,使用的比较广泛. github地址:https://github.co ...
- AutoLayout深入浅出五[UITableView动态高度]
本文转载至 http://grayluo.github.io//WeiFocusIo/autolayout/2015/02/01/autolayout5/ 我们经常会遇到UITableViewCell ...
- IOS 通过脚本自动打包工具 webfrogs/xcode_shell
博文转载至 http://www.2cto.com/kf/201506/408346.html ios 开发通过xcode 打包其实效率不是太高,所以就有人,用shell 写了一个,自动打包,发邮件, ...
- 【宝塔】 安装扩展Memcached redis 教程
宝塔官网: www.bt.cn 开始安装 1 进入ssh 输入以下指令, wget -O ext.sh http://125.88.182.172:5880/ext/ext.sh && ...
- css笔记 - 张鑫旭css课程笔记之 overflow 篇
overflow基本属性值 visible(默认值):超出依然显示 hidden :超出隐藏 scroll :超出,滚动显示.子元素不超出也会有滚动条的那条轨道. auto:如果超出,滚动显示.如果不 ...
- C++ sort函数用法 C中的qsort
需要包含#include <algorithm>MSDN中的定义: template<class RanIt> void sort(RanIt first, RanIt ...
- jQuery Sizzle选择器(一)
1.浏览器对css选择器采取逆向(从右向左)解析的原因: 如果正向解析,例如「div div p em」,我们首先就要检查当前元素到 html 的整条路径,找到最上层的div,再往下找,如果遇到不匹配 ...