爬取百度贴吧一个页面出现的问题分析

找了百度贴吧的一个页面做爬取:

#coding=utf-8
import requests
import os
import re

url='http://tieba.baidu.com/p/2460150866'

os.chdir(r'C:\Users\Administrator\Desktop\baidudata111')  # 更改工作目录为桌面

response=requests.get(url)
html=response.content
html=html.decode('utf-8')

#第一种,部分错误, 造成了部分匹配错误
reg=r'src="(.*?.jpg)" pic_ext="jpeg"'
#第二种 全部错误
#reg=r'src="(.+?)\.jpg" pic_ext'
#baidudata1
#第三种 对
#reg = r'src="(.+?\.jpg)" pic_ext'
#第四种 对
#imglist=re.findall('<img pic_type="0" class="BDE_Image" src="(http.*?jpg)".*?>',html.replace('\\',''),re.S)
#imglist=re.findall('<img pic_type="0" class="BDE_Image" src="(http.*?jpg)".*?>',html,re.S)
pattern1=re.compile(reg)
imglist=re.findall(pattern1,html)
#print(imglist)
print(len(imglist))

x=0
for imgurl in imglist:
	print(imgurl)


	bresponse=requests.get(imgurl)
	bhtml=bresponse.content

	f=open('./%s.jpg' % x,'wb')
	f.write(bhtml) 
	x=x+1

	f.close()

第一种的错误分析
抓取的链接有部分是错误的,一个匹配直到匹配到pic_ext="jpeg"才结束,例如第一个把pic_ext="bmp"和后面的jpeg的整合成一个链接匹配出来。
在这里插入图片描述在这里插入图片描述
第二种:
只把.jpg前面的链接部分匹配出来, 这部分的链接是打不开的, 故保存下来的图片都是 打不来的。
在这里插入图片描述
第三种和第四种是可以正确匹配的

初学者,问题不是一般的多呀,一个接一个,真是抓狂

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值