# -*- coding: utf-8 -*-
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/81.0.4044.122 Safari/537.36"
}
def main(url):
with requests.Session() as req:
for item in range(1, 3):
print(f"Extracting Page# {item}")
r = req.get(url.format(item), headers=headers)
soup = BeautifulSoup(r.content, 'html.parser', from_encoding='utf-8')
if 'http' in item.img['src']:
target = [[item.img['alt'], f'{item.img["src"]}']
for item in soup.select("dt.image")]
else:
target = [[item.img['alt'], f'https:{item.img["src"]}']
for item in soup.select("dt.image")]
for el in target:
print(f"{el[0]}.jpg")
r = req.get(el[1])
with open(f"{el[0]}.jpg", 'wb') as f:
f.write(r.content)
main("https://www.coupang.com/np/categories/311357?page={}")
您正在存储 item
在for循环中作为一个int。for item in range(1, 3)
. 然后你试图应用一个 .img
函数。实际上你要应用的是你的beautifulsoup对象。soup
你也需要清理你的列表理解。在其中一个例子中,你正确地连接了 'https:
而另一个,你没有。
看看这里,看看我对你的代码做了哪些修改。
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/81.0.4044.122 Safari/537.36"
}
def main(url):
with requests.Session() as req:
for item in range(1, 3):
print(f"Extracting Page# %s" %item)
r = req.get(url.format(item), headers=headers)
soup = BeautifulSoup(r.content, 'html.parser', from_encoding='utf-8')
if 'http' in soup.img['src']:
target = [[each.img['alt'], 'https:' + each.img["src"]]
for each in soup.select("dt.image")]
else:
target = [[each.img['alt'], 'https:' + each.img["src"]]
for each in soup.select("dt.image")]
for el in target:
print('%s.jpg' %el[0])
r = req.get(el[1])
with open('%s.jpg' %el[0], 'wb') as f:
f.write(r.content)
main("https://www.coupang.com/np/categories/311357?page={}")