【问题标题】:bs4 img crawler donload img pile in pythonbs4 img爬虫在python中下载img堆
【发布时间】:2020-04-24 19:29:45
【问题描述】:
import requests
import urllib
from bs4 import BeautifulSoup

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"
}

for idx in range(1, 17):
    url = "https://www.coupang.com/np/categories/311357?page=" + str(idx)

    print(url)
    result = requests.get(url, headers=headers)
    soup_obj = BeautifulSoup(result.content, "html.parser")

    div = soup_obj.findAll("div", {"class": "name"})
    lis = soup_obj.find("ul", {"id": "productList"}).findAll("li")

    for li in lis:
        name = li.find("div", {"class": "name"})
        img = li.find("dt", {"class": "image"}).find("img", {"src": ""})

        print("name: " + name.text.strip())
        urllib.request.urlretrieve(img, "./imagepile")

// urllib.request.urlretrieve(img, "./imagepile") 如何修复这行代码?多好

【问题讨论】:

  • 1.我不知道如何下载图像2。如何设置下载的堆我学习python 1 周,所以很难找到问题所在。感谢您的关注
  • 我做酒单。我要名单和图片堆

标签: beautifulsoup python-requests web-crawler urllib


【解决方案1】:
import requests
from bs4 import BeautifulSoup


headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0'}



def main(url):
    with requests.Session() as req:
        for item in range(1, 18):
            print(f"Extracting Page# {item}")
            r = req.get(url.format(item), headers=headers)
            soup = BeautifulSoup(r.content, 'html.parser')
            for item in soup.select("dt.image"):
                print(item.img['alt'], f"https:{item.img['src']}")


main("https://www.coupang.com/np/categories/311357?page={}")

下载版本:

import requests
from bs4 import BeautifulSoup


headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0'}


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')
            target = [[item.img['alt'], f'https:{item.img["src"]}']
                      for item in soup.select("dt.image")]
            for el in target:
                print(f"Saving {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={}")

【讨论】:

  • @HyeokJun 如果我的回答对您有帮助。随时通过勾选答案旁边的复选标记来接受答案:)
猜你喜欢
  • 2021-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多