【发布时间】:2020-04-27 10:26:23
【问题描述】:
# -*- 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={}")
【问题讨论】:
-
欢迎来到 StackOverflow。请查看此内容并在之后编辑您的问题:How do I ask a good question
-
从
if 'http' in item.img['src']:更改为if 'http' in soup.img['src']:
标签: python beautifulsoup web-crawler