爬虫如何爬取贴吧图片

今天携趣小编就为大家分享一下爬虫是如何爬取贴吧图片。我们可以找些图片分享吧,这样贴吧里的图片会多一些。然后打开目标帖子页面,通过源代码,分析图片位置信息。
代码如下:
import urllib.request
from bs4 import BeautifulSoup
def get_content(url):
"""取得页面内容"""
html = urllib.request.urlopen(url).read()
data = html.decode("UTF-8")
return data
def get_image(html_doc):
"""贴吧图片在<img ……> 标签下"""
soup = BeautifulSoup(html_doc)
i = 0
for link in soup.find_all("img","BDE_Image"):
i = i + 1
link_img = link.get("src")
urllib.request.urlretrieve(link_img, "%s.jpg" % i)
print(link_img)
myurl = "目标地址"
my_html_doc = get_content(myurl)
get_image(my_html_doc)
以上就是关于爬虫如何爬取贴吧图片的介绍了,点击携趣www.xiequ.cn或添加客服,共享更多讯息。