爬虫_电影天堂 热映电影(xpath)
写了一天才写了不到100行。不过总归是按自己的思路完成了
import requests
from lxml import etree
import time BASE = 'http://www.dytt8.net'
def get_one_page(url):
headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.75 Safari/537.36'}
try: response = requests.get(url, headers=headers)
response.encoding = response.apparent_encoding
return response.text
except:
return 0 def parse_one_page_href(html):
str_hrefs = []
html_element = etree.HTML(html)
# //div[@class="co_content8"]/ul/table//a/@href
hrefs = html_element.xpath('//table[@class="tbspan"]//a/@href')
for href in hrefs:
href = BASE + href
str_hrefs.append(href)
return str_hrefs """
return
['http://www.dytt8.net/html/gndy/dyzz/20180731/57193.html',
'http://www.dytt8.net/html/gndy/dyzz/20180730/57192.html',
......
'http://www.dytt8.net/html/gndy/dyzz/20180702/57064.html',
'http://www.dytt8.net/html/gndy/dyzz/20180630/57056.html']
""" def get_all_pages(page_nums):
hrefs = []
for index in range(1, page_nums + 1):
url = 'http://www.dytt8.net/html/gndy/dyzz/list_23_' + str(index) + '.html'
html = get_one_page(url)
while html == 0:
time.sleep(3)
html = get_one_page(url)
hrefs.extend(parse_one_page_href(html))
return hrefs def get_detail(page_nums):
movie = []
hrefs = get_all_pages(page_nums)
for href in hrefs: #href: every page url
informations = {} response = requests.get(href)
response.encoding = response.apparent_encoding
html = response.text html_element = etree.HTML(html) title = html_element.xpath('//font[@color="#07519a"]/text()')[0]
informations['title'] = title image_src = html_element.xpath('//p//img/@src')
informations['image_src'] = image_src[0] download_url = html_element.xpath('//td[@bgcolor="#fdfddf"]/a/@href')
informations['download_url'] = download_url texts = html_element.xpath('//div[@id="Zoom"]//p/text()')
for index, text in enumerate(texts): if text.startswith('◎片 名'):
text = text.replace('◎片 名', '').strip()
informations['english_name'] = text elif text.startswith('◎产 地'):
text = text.replace('◎产 地', '').strip()
informations['location'] = text elif text.startswith('◎上映日期'):
text = text.replace('◎上映日期', '').strip()
informations['date'] = text elif text.startswith('◎片 长'):
text = text.replace('◎片 长', '').strip()
informations['time'] = text elif text.startswith('◎导 演'):
text = text.replace('◎导 演', '').strip()
informations['director'] = text elif text.startswith('◎主 演'):
text = text.replace('◎主 演', '').strip()
actors = []
actors.append(text)
for x in range(index+1, len(texts)):
actor = texts[x].strip()
if texts[x].startswith('◎简 介'):
break
actors.append(actor)
informations['actors'] = actors elif text.startswith('◎简 介 '):
text = text.replace('◎简 介 ', '').strip()
intros = []
# intros.append(text)
for x in range(index+1, len(texts)):
intro = texts[x].strip()
if texts[x].startswith('◎获奖情况'):
break
intros.append(intro)
informations['intros'] = intros
movie.append(informations)
return movie def main():
page_nums = 1 #
movie = get_detail(page_nums)
print(movie) if __name__ == '__main__':
main()
运行结果:(选中的是一部电影, 一页中有25部电影,网站里一共有176页)

感受到了代码的魅力了吗
爬虫_电影天堂 热映电影(xpath)的更多相关文章
- python爬虫——爬取淘票票正在热映电影
今天正好学习了一下python的爬虫,觉得收获蛮大的,所以写一篇博客帮助想学习爬虫的伙伴们. 这里我就以一个简单地爬取淘票票正在热映电影为例,介绍一下一个爬虫的完整流程. 首先,话不多说,上干货——源 ...
- Node.js 抓取电影天堂新上电影节目单及ftp链接
代码地址如下:http://www.demodashi.com/demo/12368.html 1 概述 本实例主要使用Node.js去抓取电影的节目单,方便大家使用下载. 2 node packag ...
- 爬虫_豆瓣全部正在热映电影 (xpath)
单纯地练习一下xpath import requests from lxml import etree def get_url(url): html = requests.get(url) retur ...
- python爬虫——词云分析最热门电影《后来的我们》
1 模块库使用说明 1.1 requests库 requests 是用Python语言编写,基于 urllib,采用 Apache2 Licensed 开源协议的 HTTP 库.它比 urllib 更 ...
- python利用requests和threading模块,实现多线程爬取电影天堂最新电影信息。
利用爬到的数据,基于Django搭建的一个最新电影信息网站: n1celll.xyz (用的花生壳动态域名解析,服务器在自己的电脑上,纯属自娱自乐哈.) 今天想利用所学知识来爬取电影天堂所有最新电影 ...
- LOL电影天堂下载攻略
LOL电影天堂&&飘花电影网下载攻略 CreateTime--2017年7月27日08:52:29Author:Marydon 以进击的巨人为例 下载地址:http://www.l ...
- Python多线程爬虫爬取电影天堂资源
最近花些时间学习了一下Python,并写了一个多线程的爬虫程序来获取电影天堂上资源的迅雷下载地址,代码已经上传到GitHub上了,需要的同学可以自行下载.刚开始学习python希望可以获得宝贵的意见. ...
- scrapy电影天堂实战(二)创建爬虫项目
公众号原文 创建数据库 我在上一篇笔记中已经创建了数据库,具体查看<scrapy电影天堂实战(一)创建数据库>,这篇笔记创建scrapy实例,先熟悉下要用到到xpath知识 用到的xpat ...
- 用python+selenium抓取豆瓣电影中的正在热映前12部电影并按评分排序
抓取豆瓣电影(http://movie.douban.com/nowplaying/chengdu/)中的正在热映前12部电影,并按照评分排序,保存至txt文件 #coding=utf-8 from ...
随机推荐
- webpack之loader和plugin简介
webpack之loader和plugin简介 webpack入门和实战(二):全面理解和运用loader和plugins webpack入门(四)——webpack loader 和plugin w ...
- Diverse Garland CodeForces - 1108D (贪心+暴力枚举)
You have a garland consisting of nn lamps. Each lamp is colored red, green or blue. The color of the ...
- Linux下安装redis的详细过程(redis版本为4.0.10)
1.安装redis步骤 1.推荐进入到linux路径/usr/local/src 2.$ wget http://download.redis.io/releases/redis-4.0.10.tar ...
- windows 环境下 eclipse + maven + tomcat 的 hello world 创建和部署
主要记录自己一个新手用 eclipse + maven + tomcat 搭建 hello world 的过程,以及遇到的问题.讲真都是自己通过百度和谷歌一步步搭建的项目,没问过高手,也没高手可问,由 ...
- Java ME Technology - CDC(Connected Device Configuration)
Java ME Technology - CDChttps://www.oracle.com/technetwork/java/javame/tech/index-jsp-139293.html Ne ...
- 熟悉pyspider的装饰器
熟悉pyspider的装饰器取经地点:https://segmentfault.com/a/1190000002477863 @config(age=10 * 24 * 60 * 60) 在这表示我们 ...
- phantomjs 了解
转自:http://www.cnblogs.com/lei0213/ PhantomJS是一个无界面的,可脚本编程的WebKit浏览器引擎.它原生支持多种web 标准:DOM 操作,CSS选择器,JS ...
- oninput事件和onchange事件区别
onchange事件 触发条件:在域内容更改时触发,也可用于单选框和复选框改变后触发 作用对象:select.input.textarea oninput事件 触发条件:在域内容更改时触发(严格说在用 ...
- ORA-28000: the account is locked解决办法
ORA-28000: the account is locked第一步:使用PL/SQL,登录名为system,数据库名称不变,选择类型的时候把Normal修改为Sysdba;第二步:选择myjob, ...
- JavaScript lastIndexOf() 方法
<script type="text/javascript"> var str="0000.0000.0000.0000.0000.0000.0000.&qu ...