爬虫问题
请问各位鱼油,为什么我写的正则表达式返回的是一个空列表?图片的代码是这样的:img class="j_retract" id="big_img_1593494556188" src="http://tiebapic.baidu.com/forum/w%3D580%3B/sign=57277f46d8fc1e17fdbf8c397aabf703/63d9f2d3572c11dfbd627773742762d0f703c22b.jpg
程序代码如下:
import urllib.request as g
import re
def open_url(url):
req=g.Request(url)
req.add_header('user-agent','Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36')
response=g.urlopen(req)
html=response.read().decode('utf-8')
return html
def get_img(html):
p=r'<img class="j_retract" id="big_img_+" src="[^"]+\.jpg"'
imglist=re.findall(p,html)
for each in imglist:
print(each)
if __name__=='__main__':
url='https://tieba.baidu.com/f?kw=%E5%A5%B3%E7%A5%9E&ie=utf-8&pn=50'
get_img(open_url(url)) img class="j_retract" id="big_img_1593494556188" src="http://tiebapic.baidu.com/forum/w%3D580%3B/sign=57277f46d8fc1e17fdbf8c397aabf703/63d9f2d3572c11dfbd627773742762d0f703c22b.jpg
你这个图片节点是在你点击只后才会出现的,后来重新加载的,你直接浏览然后正则匹配这个肯定匹配不到的
建议你获取每个 帖子url然后 访问,然后再爬取里面的图片 即可 啊成功了!感谢感谢
页:
[1]