【问题标题】:img crawler AttributeError: 'int' object has no attribute 'img' python 3.7.6 beautifulsoup4img爬虫AttributeError:'int'对象没有属性'img'python 3.7.6 beautifulsoup4
【发布时间】: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


【解决方案1】:

您将 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={}")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-03
    • 1970-01-01
    • 1970-01-01
    • 2020-05-31
    • 2021-11-30
    • 2020-03-26
    • 2013-04-15
    相关资源
    最近更新 更多