python爬去电影天堂恐怖片+游戏

news/2025/2/21 4:34:39/

1、爬去方式python+selenium

2、工作流程

selenium自动输入,自动爬取,建立文件夹,存入磁力链接到记事本

3、贴上代码

#!/usr/bin/Python
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from bs4 import BeautifulSoup
import os
import urllib2
import time
import random
import re
browser = webdriver.Chrome()
#browser.set_window_position(20, 40)
#browser.set_window_size(1100, 700)
browser.maximize_window() #最大化
#隐式等待
browser.implicitly_wait(10)
browser.get('http://www.dytt8.net/')
browser.find_element_by_xpath('//*[@id="header"]/div/div[3]/div[2]/div[2]/div[1]/div/div[1]/form/div[1]/p[1]/input').clear()
browser.find_element_by_xpath('//*[@id="header"]/div/div[3]/div[2]/div[2]/div[1]/div/div[1]/form/div[1]/p[1]/input').send_keys(u'恐怖')def close(browser):# 获取当前窗口句柄(窗口A)handle = browser.current_window_handle# 获取当前所有窗口句柄(窗口A、B)handles = browser.window_handles# 对窗口进行遍历for newhandle in handles:# 筛选新打开的窗口Bif newhandle!=handle:browser.switch_to_window(newhandle)# 关闭当前窗口B
            browser.close()browser.switch_to_window(handles[0])def change(browser):# 获取当前窗口句柄(窗口A)handle = browser.current_window_handle# 获取当前所有窗口句柄(窗口A、B)handles = browser.window_handles# 对窗口进行遍历for newhandle in handles:# 筛选新打开的窗口Bif newhandle!=handle:browser.switch_to_window(newhandle)def back(browser):# 获取当前窗口句柄(窗口A)handle = browser.current_window_handle# 获取当前所有窗口句柄(窗口A、B)handles = browser.window_handles# 对窗口进行遍历for newhandle in handles:# 筛选新打开的窗口Bif newhandle==handle:browser.switch_to_window(newhandle)# 关闭当前窗口B
            browser.close()browser.switch_to_window(handles[0])def backN(browser):# 获取当前窗口句柄(窗口A)handle = browser.current_window_handle# 获取当前所有窗口句柄(窗口A、B)handles = browser.window_handles# 对窗口进行遍历for newhandle in handles:# 筛选新打开的窗口Bif newhandle!=handle:browser.switch_to_window(newhandle)# 关闭当前窗口B
            browser.close()browser.switch_to_window(handles[1])close(browser)
browser.find_element_by_xpath('//*[@id="header"]/div/div[3]/div[2]/div[2]/div[1]/div/div[1]/form/div[1]/p[1]/input').clear()
browser.find_element_by_xpath('//*[@id="header"]/div/div[3]/div[2]/div[2]/div[1]/div/div[1]/form/div[1]/p[1]/input').send_keys(u'恐怖')
ele = browser.find_element_by_xpath('//*[@id="header"]/div/div[3]/div[2]/div[2]/div[1]/div/div[1]/form/div[2]/input')
##直接点击不了
#模拟点击事件
ele.send_keys(Keys.ENTER)
##成功跳转到页面
obj = BeautifulSoup(browser.page_source, 'html.parser')def fun(obj, num):list = []list = obj.find('div',{'class':'co_content8'}).find_all('table')for i, v in enumerate(list):if i<=9:href = v.find('a').get('href')title = v.find('a').text##去掉特殊的符号title  = re.sub('[\/:*?"<>|]','-',title)disk_url = 'E:/test/dytt/bt/'+title+''#开始创建文件夹if os.path.exists('E:/test/dytt/bt/'+title+''):print 'This folder already exists!'else:os.mkdir(r'E:/test/dytt/bt/'+title+'')print title#url = 'http://www.ygdy8.com'+href+''###打开一个新窗口js = " window.open('http://www.ygdy8.com"+href+"')"browser.execute_script(js) ##跳转到新页面#browser.get(url)#切换到b窗口
            change(browser)#右键点击那个链接try:qqq =browser.find_element_by_xpath('//*[@id="Zoom"]/span/table/tbody/tr/td/a')ActionChains(browser).context_click(qqq).perform()hrefs = browser.find_element_by_xpath('//*[@id="Zoom"]/span/table/tbody/tr/td/a').get_attribute('href')print hrefsfile = open('E:\\test\\dytt\\bt\\'+title+'\\bt.txt', 'w')file.write(hrefs)file.close()except:print 'WE can try another way!'try:qqq =browser.find_element_by_xpath('//*[@id="Zoom"]/span/div[5]/table/tbody/tr/td/a')ActionChains(browser).context_click(qqq).perform()hrefs = browser.find_element_by_xpath('//*[@id="Zoom"]/span/div[5]/table/tbody/tr/td/a').get_attribute('href')print hrefsfile = open('E:\\test\\dytt\\bt\\'+title+'\\bt.txt', 'w')file.write(hrefs)file.close()except:print 'This is a game!'back(browser)        #循环完之后if num==0:browser.find_element_by_xpath('//*[@id="header"]/div/div[3]/div[3]/div[2]/div[2]/div[2]/ul/table[11]/tbody/tr/td[9]/a').click()else:browser.find_element_by_xpath('//*[@id="header"]/div/div[3]/div[3]/div[2]/div[2]/div[2]/ul/table[11]/tbody/tr/td[10]/a').click()change(browser)backN(browser)obj = BeautifulSoup(browser.page_source, 'html.parser')fun(obj, 1)def get_html(url):'''获取html'''##定义headersuser_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"headers={"User-Agent":user_agent}request = urllib2.Request(url, headers=headers)#request.encoding = 'utf-8'try:html = urllib2.urlopen(request).read()except urllib2.URLError as e:print url+'Download error:', e.reasonhtml = Nonereturn htmlfun(obj, 0)

 

转载于:https://www.cnblogs.com/shuangzikun/p/taotao_python_dytt.html


http://www.ppmy.cn/news/364073.html

相关文章

html5语义化边框,Html5、Css3、ES6的新特性

html5的新特性 1.语义化标签有利于seo&#xff0c;有助于爬虫抓取更多的有效信息&#xff0c;爬虫是依赖于标签来确定上下文和各个关键字的权重。 语义化的html在没有css的情况下也能呈现较好的内容结构与代码结构 方便其他设备的解析 便于团队开发和维护 2.表单新特性 3.多媒体…

ts自定义迭代器

key 为 [Symbol.iterator]

百年金街,步行天堂——王府井步行街区诞生记

导读 在不断的转型发展中&#xff0c;王府井地区的规划工作在理念导向上更加注重人的感受&#xff0c;在工作对象上有了多元化的扩展&#xff0c;在技术方法上强调多学科的融合&#xff0c;在管控手段上更加重视治理导向和更新规则制定。王府井商业街这张首都商业金名片&#x…

用python实现自动化办公------爬取小说天堂所有小说

用python实现自动化办公------爬取小说天堂所有小说 摘要声明快速爬取B站、爱奇艺、网易云音乐、QQ音乐等视频小技巧一 致敬青春二 网站技术分析三 爬虫流程四 精准爬取五 分布式爬虫 摘要 所谓爬虫&#xff0c;其实就是用户和后端码农的相互较量。用户想要从服务器端爬取数据…

通过爬取美剧天堂详细介绍Scrapy 框架入门

通过爬取美剧天堂并详细介绍Scrapy 框架 前言(了解) 全文写了很多注释在标题处&#xff0c;时间充裕的可以详细看&#xff0c;需要找主要知识点的我已经标注明白了&#xff0c;直接翻到具体位置就行。Scrapy是用纯Python实现一个为了爬取网站数据、提取结构性数据而编写的应用…

java天堂,认识Java

一、基本术语简介 JVM:(Java Vritual Machine ) Java 虚拟机,它也是整个 Java 技术的核心。Java 语言的跨平台就多亏了 JVM。 JDK: (Java Development Kit Java) 开发工具包,没有 JDK 就没有办法进行 Java 程序的开发。 JRE:(Java Runtime Environment) Java 运行环境,如…

linux能虚拟化windows,虚拟机程序将轻量虚拟化带入Linux与Windows

欢迎各位阅读本篇文章&#xff0c;Linux是一套免费使用和自由传播的类Unix操作系统&#xff0c;是一个基于POSIX和UNIX的多用户、多任务、支持多线程和多CPU的操作系统。本篇文章讲述了教大家如何在Linux中计划任务&#xff0c;课课家教育平台提醒各位&#xff1a;本篇文章纯干…

Flink 滚动窗口、滑动窗口详解

1 滚动窗口(Tumbling Windows) 滚动窗口有固定的大小&#xff0c;是一种对数据进行“均匀切片”的划分方式。窗口之间没有重叠&#xff0c;也不会有间隔&#xff0c;是“首尾相接”的状态。如果我们把多个窗口的创建&#xff0c;看作一个窗口的运动&#xff0c;那就好像它在不…