求教下大神,我爬取的第一頁網址為*/6444.html,第二頁*/6444_2.html,第三頁*/6444_3.html
從第二頁開始爬取的規律容易弄,問下第一頁怎么加進去?,問下代碼要怎么修改
"""
@ description:學習python3
@ author: chz
@ datetime: 2021-03-21 15:19:27
"""
# 匯入requests 模塊
import requests
# 匯入BeautifulSoup 模塊
from bs4 import BeautifulSoup
# 匯入urllib模塊
import urllib
x = 0
def getKunvImg(page=2):
# 網站圖片地址
urlKunv = 'https://*/2019/6444_{}.html'.format(page)
# 添加headers標識講這個爬蟲程式偽裝成瀏覽器訪問
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36'}
# 發起網路請求,獲取到回傳的html
res = requests.get(urlKunv, headers=headers)
# 設定編碼為utf-8
res.encoding = 'UTF-8'
# 格式化html
soup = BeautifulSoup(res.text, 'html.parser')
# 歷遍class樣式為.img_single的標簽
for new in soup.select('.content'):
global x
# 回傳a串列標簽個數
if len(new.select('a')) > 0:
# 獲取所有img標簽里ser路徑里面的圖片
imgsrc = new.select('img')[0]['src']
# 輸出圖片地址
# print(new.select('img')[0]['src'])
# 將獲取到的src路徑里面的圖片存盤到專案下images檔案下面
urllib.request.urlretrieve(imgsrc, './images/%s.jpg' % x)
x += 1
# 輸出下載第幾張
print('正在下載第%d張' % x)
for i in range(2, 48):
# 輸出下載第幾頁
print('正在下載第{}頁'.format(i))
getKunvImg(i)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/269162.html
