Python 爬虫实例(5)—— 爬取爱奇艺视频电视剧的链接(2017-06-30 10:37)
1. 我们找到 爱奇艺电视剧的链接地址
http://list.iqiyi.com/www/2/-------------11-1-1-iqiyi--.html
我们点击翻页发现爱奇艺的链接是这样的规律
http://list.iqiyi.com/www/2/-------------11-2-1-iqiyi--.html
http://list.iqiyi.com/www/2/-------------11-3-1-iqiyi--.html
2. 我们分析 http://list.iqiyi.com/www/2/-------------11-1-1-iqiyi--.html 请求过程
3.最后 粘贴出来代码
# coding:utf-8 import re import json, time from bs4 import BeautifulSoup import sys reload(sys) sys.setdefaultencoding('utf-8') import requests session = requests.session() def aiqiyi(): for i in range(1,2000): url = "http://list.iqiyi.com/www/2/-------------11-"+ str(i) +"-1-iqiyi--.html" if i == 1: Referer = "http://list.iqiyi.com/www/2/-------------11-"+ str(i) +"-1-iqiyi--.html" else: Referer = "http://list.iqiyi.com/www/2/-------------11-" + str(i-1) + "-1-iqiyi--.html" headers = { "Host":"list.iqiyi.com", "Connection":"keep-alive", "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", "Upgrade-Insecure-Requests":"1", "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.75 Safari/537.36", "Referer":Referer, "Accept-Encoding":"gzip, deflate, sdch", "Accept-Language":"zh-CN,zh;q=0.8", } result = session.get(url=url,headers=headers).text soup = BeautifulSoup(result, 'html.parser') returnSoup = soup.find_all("div", attrs={"class": "wrapper-piclist"})[0] returnSoup1= str(returnSoup).replace('\r\n','').replace('\n','').replace(' ','').replace('"rseat="bigTitle','') href_title = re.findall('"data-widget-qidanadd="qidanadd"href="(.*?)"target="_blank"title=".*?"><imgalt="(.*?)"height="236"rseat="dsjp7"src=',str(returnSoup1)) for i in href_title: href = i[0] title = i[1] href = str(href).split('#')[0] headers = { "Connection":"keep-alive", "Accept-Encoding":"gzip, deflate, sdch", "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.75 Safari/537.36", "Accept-Language":"zh-CN,zh;q=0.8", "Host":"www.iqiyi.com", "Upgrade-Insecure-Requests":"1", } result2 = session.get(url=href,headers=headers).text result3 = str(result2).replace('\r\n','').replace(' ','').replace('\n','').replace(' ','').replace(' ','') resultre = re.findall('<pclass="site-piclist_info_title"><ahref="(.*?)"rseat="705233_title"target="_blank">(.*?)</a></p>',str(result3)) for i in resultre: print i[0],i[1],"-----------",title time.sleep(2000) aiqiyi()
输出的结果是:
C:\Python27\python.exe C:/Users/xuchunlin/PycharmProjects/study/app/project/aiqiyiUrl.py http://www.iqiyi.com/v_19rr91tio0.html 第1集 ----------- 美味奇缘 http://www.iqiyi.com/v_19rr91wki8.html 第2集 ----------- 美味奇缘 http://www.iqiyi.com/v_19rr91d9o0.html 第3集 ----------- 美味奇缘 http://www.iqiyi.com/v_19rr91c0yg.html 第4集 ----------- 美味奇缘 http://www.iqiyi.com/v_19rr90uigk.html 第5集 ----------- 美味奇缘 http://www.iqiyi.com/v_19rr90t1gc.html 第6集 ----------- 美味奇缘 http://www.iqiyi.com/v_19rr8qb2ow.html 第7集 ----------- 美味奇缘 http://www.iqiyi.com/v_19rr8q9fi4.html 第8集 ----------- 美味奇缘 http://www.iqiyi.com/v_19rr8pr00c.html 第9集 ----------- 美味奇缘 http://www.iqiyi.com/v_19rr8p0qdc.html 第10集 ----------- 美味奇缘 http://www.iqiyi.com/v_19rr8occ3k.html 第11集 ----------- 美味奇缘 http://www.iqiyi.com/v_19rr8npdfc.html 第12集 ----------- 美味奇缘 http://www.iqiyi.com/v_19rr8nq9qw.html 第13集 ----------- 美味奇缘 http://www.iqiyi.com/v_19rr8mzwhw.html 第14集 ----------- 美味奇缘 http://www.iqiyi.com/v_19rr8n3c9k.html 第15集 ----------- 美味奇缘 http://www.iqiyi.com/v_19rr8mcd7o.html 第16集 ----------- 美味奇缘 http://www.iqiyi.com/v_19rr8mczus.html 第17集 ----------- 美味奇缘 http://www.iqiyi.com/v_19rr8lscsk.html 第18集 ----------- 美味奇缘 http://www.iqiyi.com/v_19rr8lq2c8.html 第19集 ----------- 美味奇缘 http://www.iqiyi.com/v_19rr8v32rw.html 第20集 ----------- 美味奇缘 http://www.iqiyi.com/v_19rr8uhouw.html 第21集 ----------- 美味奇缘 http://www.iqiyi.com/v_19rr8tpcss.html 第22集 ----------- 美味奇缘 http://www.iqiyi.com/v_19rr8t439c.html 第23集 ----------- 美味奇缘 http://www.iqiyi.com/v_19rr8t1sdw.html 第24集 ----------- 美味奇缘 http://www.iqiyi.com/v_19rr8sgdug.html 第25集 ----------- 美味奇缘 http://www.iqiyi.com/v_19rr8sjsvc.html 第26集 ----------- 美味奇缘 http://www.iqiyi.com/v_19rr8rx3j8.html 第27集 ----------- 美味奇缘 http://www.iqiyi.com/v_19rr8rvxm0.html 第28集 ----------- 美味奇缘 http://www.iqiyi.com/v_19rr8r96f0.html 第29集 ----------- 美味奇缘 http://www.iqiyi.com/v_19rr8rdces.html 第30集 ----------- 美味奇缘 http://www.iqiyi.com/v_19rr8qrzs4.html 第31集 ----------- 美味奇缘 http://www.iqiyi.com/v_19rrdzwrsw.html 第32集 ----------- 美味奇缘 http://www.iqiyi.com/v_19rr8qw4r4.html 第33集预告 ----------- 美味奇缘 http://www.iqiyi.com/v_19rr8qqp08.html 第34集预告 ----------- 美味奇缘 http://www.iqiyi.com/v_19rr8n78oo.html 第35集预告 ----------- 美味奇缘 http://www.iqiyi.com/v_19rr92rmic.html 第36集预告 ----------- 美味奇缘 1
如果觉得对您有帮助,麻烦您点一下推荐,谢谢!
好记忆不如烂笔头
好记忆不如烂笔头
分类:
Python 爬虫实例
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术