写了一天才写了不到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页)

感受到了代码的魅力了吗

最新文章

  1. Android将应用调试log信息保存在SD卡
  2. 自学C++第一天
  3. Python操作Excel
  4. Android 内存分析
  5. FORTRAN程序设计权威指南
  6. python文件操作汇总
  7. 《UML和模式应用》重点之思想篇
  8. $cordovaCamera 插件 上传头像 图片功能
  9. 个人练习:使用HTML+CSS制作二级菜单
  10. SpringMvc父子容器
  11. CS224d 单隐层全连接网络处理英文命名实体识别tensorflow
  12. ABBYY FineReader Pro for Mac有哪些特性(下)
  13. zookeeper(3) zookeeper的实践及原理
  14. R语言学习笔记:sort、rank、order、arrange排序函数
  15. VS2010一调试就卡死的问题解决方案 (转)
  16. Jquery获取属性值
  17. 1.4 Matplotlib:绘图
  18. [BZOJ4539][HNOI2016]树(主席树)
  19. 【2018沈阳赛区网络预选赛J题】Fantastic Graph 【有上下界的网络流】
  20. 反省在北京某S2B2C电商小型公司面试时掉链子的问题

热门文章

  1. Really Big Numbers CodeForces - 817C (数学规律+二分)
  2. github导入文件操作
  3. mysql常用命令小结
  4. hibernate 关于hbm.xml编写的总结
  5. 学习yii2.0——依赖注入
  6. php开发之系统函数
  7. [转帖]cnblog 新闻 : 亚太云计算市场报告:腾讯位列前五 份额首超谷歌
  8. JavaMail入门第一篇 邮件简介及API概述
  9. Linux基础学习(10)--Shell基础
  10. 自定义Attribute类