博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python 2.7_Second_try_爬取阳光电影网_获取电影下载地址并写入文件 20161207
阅读量:4708 次
发布时间:2019-06-10

本文共 1809 字,大约阅读时间需要 6 分钟。

1、昨天文章 是获取电影网站主菜单 然后获取每个菜单下的电影url 

2、今天是对电影url 进行再次解析获取下载地址 并写入文件  

 

4、python 代码 

#coding:utf-8import requestsfrom bs4 import BeautifulSoup as bs#爬取入口rooturl="http://www.ygdy8.com/index.html"#获取网页源码res=requests.get(rooturl)#网站编码gb2312res.encoding='gb2312'#网页源码html=res.textsoup=bs(html,'html.parser')cate_urls = []for cateurl in soup.select('.contain ul li a'):    #网站分类标题    cate_name=cateurl.text.encode('utf-8')    #分类url 进行再次爬取    cate_url="http://www.ygdy8.com/"+ cateurl['href']    cate_urls.append(cate_url)    print "网站一级菜单:",cate_name,"菜单网址:",cate_url    # newdir = "E:/moive24/"+ cate_name    # os.makedirs(newdir.decode("utf-8"))    # print "创建分类目录成功------" + newdir#每个菜单url 解析for i in range(len(cate_urls)):    cate_listurl=cate_urls[i]    res = requests.get(cate_listurl)    res.encoding = 'gb2312'    html = res.text    soup = bs(html, 'html.parser')    print "正在解析第"+str(i+1)+"个链接",cate_urls[i]    contenturls=[]    contents=soup.select('.co_content8 ul')[0].select('a')    #print contents    for title in contents:        moivetitle=title.text.encode('utf-8')        moiveurl="http://www.ygdy8.com/"+ title['href']        contenturls.append(moiveurl)        print moivetitle,moiveurl        # file_name=newdir +'/'+ moivetitle +'.txt'        # print file_name        # f = open(file_name.decode("utf-8"), "wb")        # f.close()        res = requests.get(moiveurl)        res.encoding = 'gb2312'        html = res.text        soup = bs(html, 'html.parser')        moive_sources=soup.select('#Zoom span tbody tr td a')        for source in moive_sources:            moive_source=source['href']            #print moive_source            f=open('E:/moive24/moive.txt','a')            f.write(moive_source.encode("utf-8") + "\n")            f.close

  

转载于:https://www.cnblogs.com/Mr-Cxy/p/6143029.html

你可能感兴趣的文章
15模块-Maps【管理地图控件】
查看>>
runtime
查看>>
VS2008中宽字节和普通字节的使用
查看>>
父类 子类 构造方法
查看>>
vs2015下编译duilib的几个问题
查看>>
获取周的日期范围
查看>>
css案例学习之盒子模型
查看>>
postMan模拟get和post请求,支持局域网和外网
查看>>
day16T3改错记
查看>>
Linux 安装 JDK 8
查看>>
ASP.NET Core根据环境切换NLog配置
查看>>
高质量程序设计指南c++/c语言(20)--符号常量
查看>>
strncpy实现
查看>>
华为机试——字符倒叙输出
查看>>
SQLite3中dos命令下退出"...>"状态的方法
查看>>
json/xml processing model与xml和json的简要区别
查看>>
通过Html5 Canvas画柱状图
查看>>
青蛙跳台阶(Fibonacci数列)
查看>>
洛谷P3834 [模板]可持久化线段树1(主席树) [主席树]
查看>>
Codeforces Round #316 (Div. 2)C. Replacement(模拟)
查看>>