爬虫_电影天堂 热映电影(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 ...
随机推荐
- Wannafly挑战赛28
总结- A-开始觉得是找规律,最开始模拟当时我觉得如果L达到1e9的范围的话,岂不是要加1e9次,模拟也就没有认真写,现在想来,后面由于加的不再是1,而是我前面的值,这样相当了一个斐波那契的类型,而斐 ...
- Python Revisited Day 01
逻辑操作符 身份操作符 is a = ['AAA', 3, None] b = ['AAA', 3, None] a is b #False b = a a is b #True 身份比较速度快,原因 ...
- poj2104 主席树裸题
空间大小:n*lgn 复杂度:建树n*lgn 查询lgn #include <cstdio> #include <iostream> #include <algorit ...
- 广州商学院16级软工一班&二班-第一次作业成绩
广州商学院16级软工一班&二班-第一次作业成绩 作业地址 16软工一班 16软工二班 总结 本次作业反映了几个比较严重的问题: 不按要求阅读相应的文章,回答问题只是敷衍几句. 部分同学的版式混 ...
- hibernate异常找不到get方法org.hibernate.PropertyNotFoundException: Could not find a getter for did in class com.javakc.hibernate.manytomany.entity.CourseEntity
属性的get方法没找到,可能是CourseEntity类中对应属性没有get方法,如果有就看CourseEntity.hbm.xml属性名称,应该是写错了不和CourseEntity类中属性名相同,修 ...
- laravel log改为时间格式
1 providers新建文件 LogRotateServiceProvider.php <?php namespace App\Providers; use Monolog\Formatter ...
- Servlet--HttpServlet实现doGet和doPost请求的原理
转:https://blog.csdn.net/m0_38039437/article/details/75264012 一.HttpServlet简介 1.HttpServlet是GenericSe ...
- 关于标准的知识 GB ISO 等内容
1. 来自百度知道: GB:GB 即"国标"的汉语拼音缩写,为中华人民共和国国家标准的意思. ISO:国际标准化组织的英语简称.其全称是International Organiza ...
- js的日期操作:String转date日期格式、求日期差
一.在js中String类型转成date格式 var date = new Date("2018-9-21 14:58:43");//就是这么简单 二.date转String类型就 ...
- css3特殊图形(气泡)
一.气泡 效果: body{ background: #dd5e9d; height: 100%; } .paopao { position: absolute; width: 200px; heig ...