前言
本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理。
作者: Python新手学习之家
用python实现的抓取腾讯视频所有电影的爬虫
-
-
-
-
from bs4 import BeautifulSoup
-
-
-
-
-
-
-
-
-
-
req = urllib2.Request(url)
-
response = urllib2.urlopen(req)
-
-
-
-
在学习过程中有什么不懂得可以加我的python学习交流扣扣qun,934109170,群里有不错的学习教程与开发工具。
-
-
-
-
-
-
soup = BeautifulSoup(html)
-
-
-
tags_all = soup.find_all(‘ul‘, {‘class‘ : ‘clearfix _group‘ , ‘gname‘ : ‘mi_type‘})
-
-
-
-
-
re_tags = r‘<a _hot=\"tag\.sub\" class=\"_gtag _hotkey\" href=\"(.+?)\" title=\"(.+?)\" tvalue=\"(.+?)\">.+?</a>‘
-
p = re.compile(re_tags, re.DOTALL)
-
-
tags = p.findall(str(tags_all[0]))
-
-
-
-
-
tag_url = tag[0].decode(‘utf-8‘)
-
-
m_type = tag[1].decode(‘utf-8‘)
-
tags_url[m_type] = tag_url
-
-
-
-
-
-
-
-
tag_html = gethtml(tag_url)
-
-
soup = BeautifulSoup(tag_html)
-
-
-
div_page = soup.find_all(‘div‘, {‘class‘ : ‘mod_pagenav‘, ‘id‘ : ‘pager‘})
-
-
-
-
re_pages = r‘<a class=.+?><span>(.+?)</span></a>‘
-
p = re.compile(re_pages, re.DOTALL)
-
pages = p.findall(str(div_page[0]))
-
-
-
-
-
-
-
-
-
soup = BeautifulSoup(html)
-
-
-
divs = soup.find_all(‘ul‘, {‘class‘ : ‘mod_list_pic_130‘})
-
-
-
div_html = str(div_html).replace(‘\n‘, ‘‘)
-
-
-
-
-
-
-
-
-
-
re_movie = r‘<li><a class=\"mod_poster_130\" href=\"(.+?)\" target=\"_blank\" title=\"(.+?)\"><img.+?</li>‘
-
p = re.compile(re_movie, re.DOTALL)
-
-
-
conn = pymongo.Connection(‘localhost‘, 27017)
-
-
playlinks = movie_db.playlinks
-
-
-
-
-
print "%s : %d" % ("=" * 70, NUM)
-
-
-
-
-
-
-
-
-
-
-
print "%s : %d" % ("=" * 70, NUM)
-
-
-
-
-
-
-
soup = BeautifulSoup(html)
-
-
-
divs = soup.find_all(‘div‘, {‘class‘ : ‘pack pack_album album_cover‘})
-
-
-
-
re_info = r‘<a href=\"(.+?)\" target=\"new\" title=\"(.+?)\" wl=\".+?\"> </a>‘
-
p_info = re.compile(re_info, re.DOTALL)
-
m_info = p_info.findall(str(divs[0]))
-
-
-
-
print "Not find movie info"
-
-
-
-
-
-
-
movie_db = conn.dianying_at
-
-
-
-
if __name__ == "__main__":
-
-
-
tags_url = "http://v.qq.com/list/1_-1_-1_-1_1_0_0_20_0_-1_0.html"
-
-
tags_html = gethtml(tags_url)
-
-
tag_urls = gettags(tags_html)
-
-
-
-
for url in tag_urls.items():
-
print str(url[1]).encode(‘utf-8‘)
-
maxpage = int(get_pages(str(url[1]).encode(‘utf-8‘)))
-
-
-
for x in range(0, maxpage):
-
-
m_url = str(url[1]).replace(‘0_20_0_-1_0.html‘, ‘‘)
-
movie_url = "%s%d_20_0_-1_0.html" % (m_url, x)
-
-
movie_html = gethtml(movie_url.encode(‘utf-8‘))
-
-
-
大工告成,以上代码大家都看明白了没? 如果你看不懂,建议你可以去小编的Python交流.裙 :一久武其而而流一思(数字的谐音)转换下可以找到了,里面有最新Python教程项目,多练习自然就懂了!