爬虫抓取网上图片,运行没有报错,但是也没有结果。请问:是什么原因,如何解决呢?
import os
import requests
from bs4 import BeautifulSoup
headers = {
'User-Agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36 Edg/118.0.2088.46'
}
dirName = 'ImgLib'
if not os.path.exists(dirName):
os.mkdir(dirName)
url = 'https://www.ugg.cn/'
response = requests.get(url,headers = headers)
#print(response.text)
content = response.text
page = BeautifulSoup(content,"html.parser")
#print(page)
name_list = page.select('div[class="GoodsSelector"]')
print(name_list)
#for name in list:
# print(name.get_text())
img_url_list = page.select('img[id="img_big_0"]')
#print(img_url_list)
for img in img_url_list:
print(img)
for name in name_list:
img_name = name.get_text().replace('/','_')+'.jpg'
print(img)
img_style = img.attrs.get("style").split('""')[1].strip('/')
#
print(img_style)
img_src = 'https://www.ugg.cn/gallery-v-0-558.html' + img_style
print(img_src)
img_content = requests.get(img_src,headers = headers).content
imgpath = dirName = '/' + img_name
with open(imgpath,'wb')as fp:
fp.write(img_content)