小白学爬虫,为何没错误也没结果,求大佬。。

weixin_42781845 2019-02-20 11:49:18
不知道为什么错了


#!/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()
...全文
686 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Claude_721 2019-10-13
  • 打赏
  • 举报
回复
我也碰到了这样的问题,请问解决了吗
csb7929 2019-02-28
  • 打赏
  • 举报
回复
我用python3.6跑了一下这个代码,主要问题有两个:
1.一个是 urllib.request.urlopen(url)读取不了js动态加载网页;
2.第二个是正则匹配有点问题;
第一个问题,用Python+Selenium与Chrome就解决,需要下载配套的webdriver和chrome;
第二个问题修改正则匹配即可;
附修改后代码,python3.6调试通过:
#!/usr/bin/python
# coding:utf-8
# 实现一个简单的爬虫,爬取百度贴吧图片
import urllib.request
import re
import pdb
from selenium import webdriver

# 根据url获取网页html内容
def getHtmlContent(url):
#page = urllib.request.urlopen(url)
#return page.read()
driver=webdriver.Chrome()
driver.get(url)
page_source=driver.page_source
driver.quit()
return page_source

# 从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列表
#pdb.set_trace()
pattern='https://\S+.jpg'
jpgs = re.findall(pattern,html)
return jpgs

# 用图片url下载图片并保存成制定文件名
def downloadJPG(imgUrl,fileName):
urllib.request.urlretrieve(imgUrl,fileName)

# 批量下载图片,默认保存到当前目录下
def batchDownloadJPGs(imgUrls,path = './'):
# 用于给图片命名
#pdb.set_trace()
count = 1
for url in imgUrls:
#pdb.set_trace()
downloadJPG(url,''.join([path,'{0}.jpg'.format(count)]))
count = count + 1

# 封装:从百度贴吧网页下载图片
def download(url):
#pdb.set_trace()
html = getHtmlContent(url)
file_hnd=open('html.txt','w',encoding='utf-8')
file_hnd.write(html)
file_hnd.close()
#html_ext=html.decode('utf-8')
#i=0
#while i<5:
#index=html_ext.find('img src=')
#html_ext=html_ext[index+5:index+100]
#print (html_ext[index+5:index+100])
#i=i+1
print (len(html))
jpgs = getJPGs(html)
print (len(jpgs))
#pdb.set_trace()
batchDownloadJPGs(jpgs)

def main():
url = 'https://image.baidu.com/'
download(url)

if __name__ == '__main__':
main()
tianfang 2019-02-21
  • 打赏
  • 举报
回复
正则前后的结果集合各是多少个? 这个可以判断问题位置在正则前或者后
weixin_42781845 2019-02-21
  • 打赏
  • 举报
回复
那如何知道正则前后有几个结果集

37,719

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧