运行后结果全是一串串的名字代码,这串代码哪出现问题了,有人帮忙看一下吗
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/114.0.0.0 Safari/537.36'
}
dirName = 'ImgLib'
if not os.path.exists(dirName):
os.mkdir(dirName)
url = 'https://consumer.huawei.com/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="laptops-main-product-card"]')
print(name_list)
#for name in list:
# print(name.get_text())
img_url_list = page.select('div[class="products-display-box container init-normal"]')
#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_name)
img_style = img.attrs.get("style").split('"')[1].strip('/')
#
print(img_style)
img_src ='https://consumer.huawei.com/cn/phones/' + img_style
print(img_src)
img_content = requests.get(img_src,headrs = headers).content
imgpath = dirName + '/' + img_name
with open(imgpath,'wb')as fp:
fp.write(img_content)
运行结果: