小白学爬虫,为何没错误也没结果,求大佬。。
不知道为什么错了
#!/usr/bin/python
# coding:utf-8
# 实现一个简单的爬虫,爬取百度贴吧图片
import urllib.request
import re
# 根据url获取网页html内容
def getHtmlContent(url):
page = urllib.request.urlopen(url)
return page.read()
# 从html中解析出所有jpg图片的url
# 百度贴吧html中jpg图片的url格式为:<img ... src="XXX.jpg" width=...>
def getJPGs(html):
# 解析jpg图片url的正则
jpgReg = re.compile(r'<img src="(https://ss1.baidu.com/-4o3dSag_xI4khGko9WTAnF6hhy/image/h%3D300/.+?\.jpg)" width') # 注:这里最后加一个'width'是为了提高匹配精确度
# 解析出jpg的url列表
jpgs = re.findall(jpgReg,html.decode('utf-8'))
return jpgs
# 用图片url下载图片并保存成制定文件名
def downloadJPG(imgUrl,fileName):
urllib.urlretrieve(imgUrl,fileName)
# 批量下载图片,默认保存到当前目录下
def batchDownloadJPGs(imgUrls,path = 'C:/Users/Administrator/Desktop/pic2/'):
# 用于给图片命名
count = 1
for url in imgUrls:
downloadJPG(url,''.join([path,'{0}.jpg'.format(count)]))
count = count + 1
# 封装:从百度贴吧网页下载图片
def download(url):
html = getHtmlContent(url)
jpgs = getJPGs(html)
batchDownloadJPGs(jpgs)
def main():
url = 'https://image.baidu.com/'
download(url)
if __name__ == '__main__':
main()
#!/usr/bin/python
# coding:utf-8
# 实现一个简单的爬虫,爬取百度贴吧图片
import urllib.request
import re
# 根据url获取网页html内容
def getHtmlContent(url):
page = urllib.request.urlopen(url)
return page.read()
# 从html中解析出所有jpg图片的url
# 百度贴吧html中jpg图片的url格式为:<img ... src="XXX.jpg" width=...>
def getJPGs(html):
# 解析jpg图片url的正则
jpgReg = re.compile(r'<img src="(https://ss1.baidu.com/-4o3dSag_xI4khGko9WTAnF6hhy/image/h%3D300/.+?\.jpg)" width') # 注:这里最后加一个'width'是为了提高匹配精确度
# 解析出jpg的url列表
jpgs = re.findall(jpgReg,html.decode('utf-8'))
return jpgs
# 用图片url下载图片并保存成制定文件名
def downloadJPG(imgUrl,fileName):
urllib.urlretrieve(imgUrl,fileName)
# 批量下载图片,默认保存到当前目录下
def batchDownloadJPGs(imgUrls,path = 'C:/Users/Administrator/Desktop/pic2/'):
# 用于给图片命名
count = 1
for url in imgUrls:
downloadJPG(url,''.join([path,'{0}.jpg'.format(count)]))
count = count + 1
# 封装:从百度贴吧网页下载图片
def download(url):
html = getHtmlContent(url)
jpgs = getJPGs(html)
batchDownloadJPGs(jpgs)
def main():
url = 'https://image.baidu.com/'
download(url)
if __name__ == '__main__':
main()
...全文
相关推荐
基于Python网络爬虫毕业论文.doc
这是一份同学的爬虫的毕业论文,完整的。需要的赶紧拿走
C#网络爬虫程序源码
网上找的一个C#写的网络爬虫程序源码,支持多线程,上传上来方便自己查看,有需要的朋友可以直接拿去参考,呵呵~~~谢谢作者。
网页内容自动抓取工具-我的网络爬虫
自己写的网络爬虫工具,配置了只对某些论坛抓取,自己可以改写配置文件来抓取其他网页内容...
VC++实现的网络爬虫程序,非常适合做二次开发
VC++实现的网络爬虫程序,非常好,适合作为框架进行二次开发
Python网络爬虫技术 完整教案.docx
Python网络爬虫技术 完整教案.docxPython网络爬虫技术 完整教案.docxPython网络爬虫技术 完整教案.docxPython网络爬虫技术 完整教案.docxPython网络爬虫技术 完整教案.docxPython网络爬虫技术 完整教案.docxPython网络爬虫技术 完整教案.docxPython网络爬虫技术 完整教案.docx


