利用urllib和xpath 爬取电影播放地址
本帖最后由 E飞翔 于 2019-3-7 00:33 编辑好久没有写代码了,今天闲着就来写写这个。
若写的不好请大神指点,谢谢!
from urllib import request
from lxml import etree
from pprint import pprint
class GetMoviePlay:
"""
# 取电影标题 '//*[@id="data_list"]/tr/td/a'
# 取电影页面链接 '//*[@id="data_list"]/tr/td/a/@href'
# 取电影类型 '//*[@id="data_list"]/tr/td/a'
# 取电影地区 '//*[@id="data_list"]/tr/td/p/font'
# 取连载状态 '//*[@id="data_list"]/tr/td/span/font'
# 取更新时间 '//*[@id="data_list"]/tr/td/font'
# 取当前页码详情 '/html/body/div/div/div/text()'
# 获取真实链接 /html/body/div/div/div/div/ul/li/text()
"""
def __init__(self,page):
self.__url = 'http://yongjiuzy.cc/?m=vod-type-id-6-pg-%d.html' % page
self.__name = (
'//*[@id="data_list"]/tr/td/a/text()',
'//*[@id="data_list"]/tr/td/a/@href',
'//*[@id="data_list"]/tr/td/a/text()',
'//*[@id="data_list"]/tr/td/p/font/text()',
'//*[@id="data_list"]/tr/td/span/font/text()',
'//*[@id="data_list"]/tr/td/font/text()',
'/html/body/div/div/div/text()',
'/html/body/div/div/div/div/li/text()',
'/html/body/div/div/div/div/ul/li/text()'
)
@property
def movie_info_dict(self):
res = request.Request(self.__url)
response = request.urlopen(res).read()
response = etree.HTML(response)
# 取电影标题
m_cont_list_tmp = response.xpath(self.__name)
m_cont_list = []
# 解析电影标题,只取奇数的主标题
for i in range(len(m_cont_list_tmp)):
if i % 2 == 0:
m_cont_list.append(m_cont_list_tmp)
# 取电影页面链接
m_url_list = response.xpath(self.__name)
# 拼接url
for i in range(len(m_url_list)):
m_url_list = 'http://yongjiuzy.cc/' + m_url_list
# 取电影类型
m_type_list = response.xpath(self.__name)
# 取电影地区 '//*[@id="data_list"]/tr/td/p/font'
m_city_list = response.xpath(self.__name)
# 取连载状态 '//*[@id="data_list"]/tr/td/span/font'
m_state_list = response.xpath(self.__name)
# 取更新时间 '//*[@id="data_list"]/tr/td/font'
m_time_list = response.xpath(self.__name)
# 取当前页码详情 '/html/body/div/div/div/text()'
m_this_page = response.xpath(self.__name)
return {'context':m_cont_list,
'url':m_url_list,
'type':m_type_list,
'city':m_city_list,
'state':m_state_list,
'time':m_time_list,
'thisPage':m_this_page
}
def getMovieTrue(self,url):
res = request.Request(url)
response = request.urlopen(res).read()
response = etree.HTML(response)
# 电影名称 /html/body/div/div/div/div/li/text()
m_true_context = response.xpath(self.__name)
# 获取真实链接 /html/body/div/div/div/div/ul/li/text()
m_true_url = response.xpath(self.__name)
# 返回列表数据
return ,m_true_url]
if __name__ == '__main__':
a = GetMoviePlay(1) # 初始化一个页码
# 返回了一堆数据,其实我就只要了url
url = a.movie_info_dict['url']
for i in range(len(url)):
info = a.getMovieTrue(url)
pprint(info)
学习了,收听下。{:1_893:} 有意思。。。 学习学习 xpath不错的 学习学习 不错不错,跟着大大的源码学习学到了不少知识。感谢分享技术。 学习使我快乐 xpath的选择器全写在对象的属性里有什么好处吗?是为了美观?突出本贴的标题?还是别的什么? 测试下~~~
页:
[1]