简单的网络爬虫代码
程序员文章站
2022-05-06 20:32:20
...
import urllib.request
import re
import os
import urllib
def getHtml(url):
page = urllib.request.urlopen(url)
html = page.read()
return html.decode('UTF-8')
def getImg(html):
reg = r'src="(.+?\.jpg)" pic_ext'
imgre = re.compile(reg)
imglist = imgre.findall(html)
x = 0
path = 'D:\\test'
if not os.path.isdir(path):
os.makedirs(path)
paths = path+'\\'
for imgurl in imglist:
urllib.request.urlretrieve(imgurl,'{}{}.jpg'.format(paths,x))
x = x + 1
return imglist
html = getHtml("http://tieba.baidu.com/p/2460150866")
print (getImg(html))
上一篇: 深圳智慧探索科技有限公司笔试题总结