今天写了一个关于捧腹网的爬虫
1、先上代码:# coding:utf-8
import urllib.request
import re
x = 0#下载序号
for pg in range(0,3): # 先来3页测试
pg += 1 # 页数
url = 'http://www.pengfu.com/index_' + str(pg) + '.html'
html = urllib.request.urlopen(url).read().decode('utf-8') # 获取网页源码
title = re.compile(r'<h1 class="dp-b"><a href=".*?" target="_blank">(.*?)</a>',re.S)
titles = re.findall(title,html) # 匹配title
img = re.compile(r'<img src="(.*?)" width=')
imgs = re.findall(img,html) # 匹配图片
for k,v in zip(titles,imgs):
print(k,v)
path = 'D:\Python之窗\Spider\pengfu\%s.%s.jpg' %(x,k) #.decode('utf-8').encode('gbk')
urllib.request.urlretrieve(v,path)
x += 1 # 名称序号
'''文字和图片不匹配'''
2、运行结果:
C:\Python\Python35\python.exe D:/Python之窗/Spider/pengfu.py
这身搭配非常显身材。 http://image5.pengfu.com/origin/161230/5866058cc7886.jpg
打喷嚏打的停不下来 http://image5.pengfu.com/thumb/161230/5865f43bb8efc.jpg
左边的女生是来送人头的吧? http://image1.pengfu.com/thumb/161230/5865e923780bf.jpg
求个名字,捧腹人才多 http://image3.pengfu.com/origin/161230/5865db97bdc05.jpg
【今日话题】毁童年的动画你看过哪些? http://image4.pengfu.com/origin/161230/5865d47a3a995.jpg
很喜欢他们 http://image4.pengfu.com/origin/161230/5865b89014f6e.jpg
这个逼装的,无懈可击 http://image4.pengfu.com/thumb/161230/5865f428e66fb.jpg
哥哥回来了 http://image4.pengfu.com/origin/161230/5865cd1a47a81.jpg
这样秀恩爱好烧钱啊! http://image4.pengfu.com/origin/161230/5865d323150cb.png
果然是亲生的,姿势都一毛一样 http://image2.pengfu.com/origin/161230/5865b3250dba9.jpg
取个款也太小心了吧! http://image2.pengfu.com/thumb/161230/586605721add7.jpg
大人的东西不要随便乱动! http://image5.pengfu.com/origin/161230/5865cdd4ae469.jpg
真的假的? http://image5.pengfu.com/origin/161229/5864d56e288fe.jpg
完蛋了这给老婆带的生日礼物啊 http://image1.pengfu.com/origin/161230/586601f4a26b6.png
被原配殴打的小三,路人竟然劝和 http://image3.pengfu.com/origin/161229/5864d64fb917a.jpg
出门前一定要检查一下自己的着装啊 http://image2.pengfu.com/origin/161230/5865cf17dba10.jpg
美女,注定是与众不同的 http://image2.pengfu.com/origin/161229/5864d694e2b93.jpg
迟到了悄悄走进教室却发现走错了的我 http://image3.pengfu.com/origin/161229/5864d5002d65d.jpg
当买家秀遇到了卖家秀 http://image1.pengfu.com/origin/161229/5864d57b2b1b5.jpg
来吧,来场公平的决斗 http://image4.pengfu.com/thumb/161230/5865f3ff3f9f2.jpg
这俩碗牛肉面花了我250块 http://image3.pengfu.com/origin/161229/5864d5ce755c7.jpg
进程已结束,退出代码0
3、下载结果:
4、说说写完后的疑点:
a、比喻一个贴吧,如果不知道尾页,for...range()此时不能用,多页效果如何达到
b、名称和地址不匹配(有不对应的组合存在)
5、是不是在匹配title和img时得先确保是一个组合才不会出现titles和imgs不对等的关系,从而导致文字和图片的不对等
厉害了,兄弟 塔兹米888 发表于 2016-12-30 22:45
厉害了,兄弟
代码还得优化,不够完善 看看 {:10_256:}{:10_256:}{:10_256:}{:10_256:}手动羡慕,还在学习中 关注了 李金龙 发表于 2017-1-3 22:02
手动羡慕,还在学习中
向小鱼儿看齐 认真看图片去了。哈哈 哈哈全程在看图。。。
页:
[1]