欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

简单的网络爬虫代码

程序员文章站 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))