1. 程式人生 > >python爬蟲【例項】爬取豆瓣電影評分連結並圖示()-問題如何爬取電影圖片(解決有程式碼)

python爬蟲【例項】爬取豆瓣電影評分連結並圖示()-問題如何爬取電影圖片(解決有程式碼)

這裡只有尾巴,來分析一下

確定範圍:




如何爬取圖片並下載?

參考:http://blog.csdn.net/chaoren666/article/details/53488083





--------------------------------------------------------------放棄這個方法

畢竟我用的都是python3

-------------------------------------------------------

參考:http://blog.csdn.net/wickedvalley/article/details/51992245



解決錯問題:http://blog.csdn.net/tzs_1041218129/article/details/52228905

解決方法是:把’html’型別調整一下:html.decode(‘utf-8’)

-------------------------------------------------------------------------------------------



然後差不多了:



終於成功了:


不過又報錯了點:


上程式碼:

#coding=utf-8
import urllib
#在python3.3裡面,用urllib.request代替urllib2
import urllib.request
import re
from bs4 import BeautifulSoup

url = "http://tieba.baidu.com/p/2460150866"# 自己改吧
page = urllib.request.urlopen(url)
html = BeautifulSoup(page,'lxml')
print(html.prettify())


#正則匹配
reg = r'src="(.+?\.jpg)"'
imgre = re.compile(reg)
imglist = re.findall(imgre, html.decode('utf-8'))
x = 0
print("start dowload pic")

for imgurl in imglist:
    print(imgurl)
    resp = urllib.request.urlopen(imgurl)
    respHtml = resp.read()
    picFile = open('E:\\img\\%s.jpg' % x,'wb')
    picFile.write(respHtml)
    picFile.close()
    x = x+1

print("done")