不知道為什么錯了
#!/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="https://img.uj5u.com/2020/10/04/148406040238501.jpg" width=...>
def getJPGs(html):
# 決議jpg圖片url的正則
jpgReg = re.compile(r'<img src="https://bbs.csdn.net/topics/(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()
uj5u.com熱心網友回復:
正則前后的結果集合各是多少個?這個可以判斷問題位置在正則前或者后
uj5u.com熱心網友回復:
那如何知道正則前后有幾個結果集uj5u.com熱心網友回復:
我用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="https://img.uj5u.com/2020/10/04/148406040238501.jpg" width=...>
def getJPGs(html):
# 決議jpg圖片url的正則
#jpgReg = re.compile(r'<img src="https://bbs.csdn.net/topics/(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='https://bbs.csdn.net/topics/)
#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()
uj5u.com熱心網友回復:
我也碰到了這樣的問題,請問解決了嗎
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/153192.html
上一篇:SQLServer相關操作
