import requests
import re#根据url获取网页html内容
def getHtmlContent(url):page = requests.get(url)return page.texthtml = getHtmlContent('https://tieba.baidu.com/p/4840106397')#以html中使用re模块解析出所有jpg图片的url
#百度贴吧html中jpg图片的url格式:<img...src="xxx.jpg" width=....>
def getJPGs(html):#解析jpg图片url的正则jpgReg = re.compile(r'<img.*?src="(.*?\.jpg)"')#解析出jpg的url列表jpgs = re.findall(jpgReg,html)return jpgsjpgs = getJPGs(html)
print(jpgs)html = 'https://tieba.baidu.com/f?ie=utf-8&kw=%E5%9B%BE%E7%89%87&fr=search'#用图片url下载图片并保存成制定文件名
def downloadJPG(imgUrl,fileName):#可自动关闭请求和相应的模块from contextlib import closingwith closing(requests.get(imgUrl,stream=True)) as resp:with open(fileName,'wb') as f:for chunk in resp.iter_content(128):f.write(chunk)#批量下载图片,默认保存在当前目录下
def batchDownloadJPGs(imgUrls,path='./'):#用于给图片命名count =1for url in imgUrls:downloadJPG(url,''.join([path,'{0}.jpg'.format(count)]))print('下载完成第{0}张图片'.format(count))count =count+1#封装,从百度贴吧网页下载图片
def download(url):html = getHtmlContent(url)jpgs = getJPGs(html)batchDownloadJPGs(jpgs)def main():url = 'http://tieba.baidu.com'download(url)if __name__ =='__main__' :main()
进入爬虫学习:
首先导包
import requests
import re
获取网页源码,确定可以获取,获得了之后可以操作的整体对象
#根据url获取网页html内容
def getHtmlContent(url):page = requests.get(url)return page.texthtml = getHtmlContent('https://tieba.baidu.com/p/4840106397')
获取图片,用到的就是很神奇的正则表达式,本人起初觉得很一般,没什么不得了,但是用的时候,却发现大道至简。
#以html中使用re模块解析出所有jpg图片的url
#百度贴吧html中jpg图片的url格式:<img...src="xxx.jpg" width=....>
def getJPGs(html):#解析jpg图片url的正则jpgReg = re.compile(r'<img.*?src="(.*?\.jpg)"')#解析出jpg的url列表jpgs = re.findall(jpgReg,html)return jpgsjpgs = getJPGs(html)
print(jpgs)html = 'https://tieba.baidu.com/f?ie=utf-8&kw=%E5%9B%BE%E7%89%87&fr=search'
接下来就是获取图片,照着跑就行。
#用图片url下载图片并保存成制定文件名
def downloadJPG(imgUrl,fileName):#可自动关闭请求和相应的模块from contextlib import closingwith closing(requests.get(imgUrl,stream=True)) as resp:with open(fileName,'wb') as f:for chunk in resp.iter_content(128):f.write(chunk)#批量下载图片,默认保存在当前目录下
def batchDownloadJPGs(imgUrls,path='./'):#用于给图片命名count =1for url in imgUrls:downloadJPG(url,''.join([path,'{0}.jpg'.format(count)]))print('下载完成第{0}张图片'.format(count))count =count+1#封装,从百度贴吧网页下载图片
def download(url):html = getHtmlContent(url)jpgs = getJPGs(html)batchDownloadJPGs(jpgs)def main():url = 'http://tieba.baidu.com'download(url)if __name__ =='__main__' :main()