RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1095968
Accepted
Lev145
Lev145
Asked:2020-03-18 02:12:29 +0000 UTC2020-03-18 02:12:29 +0000 UTC 2020-03-18 02:12:29 +0000 UTC

脚本不适用于 BeautifulSoup 和请求 (Python3x)

  • 772

我试图解析网站上的图片地址(https://amitego.ru/)

结果是这样的:

import requests
from bs4 import BeautifulSoup

HEADERS = {
'user-agent': '*Скрыто*',
'accept': '*/*'

}

def get_html(url):
    r = requests.get(url, headers=HEADERS)
    return r.text

def get_images(html):
    soup = BeautifulSoup(html, 'lxml')
    a = soup.find('div', class_='pp-posts-container').find_all('div', class_='pp-post-thumbnail')
    images = []

    for i in images:
        img = a.find('a').get('src')
        images.append(a)
    return images



def main():
    url = 'https://amitego.ru/'
    all_images = get_images(get_html(url))

    for i in all_images:
        print(i)

if __name__ == '__main__':
    main()

在此处输入图像描述 解析时,它不会产生任何东西,我不知道为什么( 在此处输入图像描述 请告诉我我可能错在哪里。

python-3.x
  • 2 2 个回答
  • 10 Views

2 个回答

  • Voted
  1. Best Answer
    nomnoms12
    2020-03-18T05:54:31Z2020-03-18T05:54:31Z

    这些行中有几个错误:

    a = soup.find('div', class_='pp-posts-container').find_all('div', class_='pp-post-thumbnail')
    images = []
    
    for i in images:
        img = a.find('a').get('src')
        images.append(a)
    

    在一个循环中,for您正在遍历一个空列表。也就是说,根本不执行循环,函数返回一个空列表images。

    但是,即使您纠正错误并迭代a,也不会产生任何结果,因为i您根本不使用该变量,而且您获得的链接不是指向图像,而是指向文章。

    工作示例:

    import requests
    from bs4 import BeautifulSoup
    
    
    def parse():
        html = requests.get('https://amitego.ru/').text
        soup = BeautifulSoup(html, 'lxml')
    
        div_tags = soup.find_all('div', {'class': 'pp-post-thumbnail'})
        img_tags = [div.find('img') for div in div_tags]
    
        image_src = [img['data-src'] for img in img_tags]
        return image_src
    
    
    def main():
        links = parse()
        print(*links, sep='\n')
    
    
    if __name__ == '__main__':
        main()
    

    标准输出:

    https://amitego.ru/wp-content/uploads/2020/03/gettyimages-1189261010-scaled.jpg
    https://amitego.ru/wp-content/uploads/2020/02/litedesk-1.png
    https://amitego.ru/wp-content/uploads/2020/02/1472905475_sd_maid_pro.jpg
    https://amitego.ru/wp-content/uploads/2020/02/Z0JMHoqnCO4.jpg
    https://amitego.ru/wp-content/uploads/2020/02/intel-neuromorphic-system-loihi.jpeg
    https://amitego.ru/wp-content/uploads/2020/02/Windows-10-20H1.jpg
    
    • 0
  2. Ole Lukøje
    2020-03-18T06:22:22Z2020-03-18T06:22:22Z
    import wget
    import requests
    from bs4 import BeautifulSoup as Soup
    
    if __name__ == '__main__':
        with requests.Session() as session:
            soup = Soup(session.get('https://amitego.ru/').content, 'html.parser')
    
            pages = int(soup.find('div', {'class': 'pp-posts-pagination-wrap'}).extract()['data-total']) + 1
    
            links = []
    
            for num in range(1, pages):
                page = Soup(session.get(f'https://amitego.ru/?page={num}').content, 'html.parser')
                links.extend([item.attrs['data-srcset'].split(',')[0].split()[0].strip()
                              for item in page.find_all('img', {'class': 'size-full'}) if 'data-srcset' in item.attrs])
    
            print(*links, sep='\n')
    

    如果相反

    print(*links, sep='\n')
    

    添加

    for link in links:
        wget.download(link, bar=None)
    

    然后代码将加载图像

    有6页,代码解析全部6页,下面是输出:

    https://amitego.ru/wp-content/uploads/2020/03/gettyimages-1189261010-scaled.jpg
    https://amitego.ru/wp-content/uploads/2020/02/litedesk-1.png
    https://amitego.ru/wp-content/uploads/2020/02/1472905475_sd_maid_pro.jpg
    https://amitego.ru/wp-content/uploads/2020/02/Z0JMHoqnCO4.jpg
    https://amitego.ru/wp-content/uploads/2020/02/intel-neuromorphic-system-loihi.jpeg
    https://amitego.ru/wp-content/uploads/2020/02/Windows-10-20H1.jpg
    https://amitego.ru/wp-content/uploads/2020/01/Android-Security-Patch-September-2017.jpg
    https://amitego.ru/wp-content/uploads/2020/01/Аннотация-2020-01-29-232035.png
    https://amitego.ru/wp-content/uploads/2020/01/004fe181da1577c5677f662bf5d3c260.jpg
    https://amitego.ru/wp-content/uploads/2020/01/i.jpg
    https://amitego.ru/wp-content/uploads/2020/01/52d30b22b88a476ee5aa97bba1aa82da.jpg
    https://amitego.ru/wp-content/uploads/2020/01/kisspng-television-set-download-retro-old-antenna-tv-set-5a799c8ad3ab07.109004031517919370867-1.png
    https://amitego.ru/wp-content/uploads/2020/01/Без-названия.jpeg
    https://amitego.ru/wp-content/uploads/2019/12/Is-It-Too-Early-to-Start-Planning-For-2020-Tech-Critic-864x576-1.jpg
    https://amitego.ru/wp-content/uploads/2019/12/Dr5whIHWoAIs5sI.jpg-large.jpg
    https://amitego.ru/wp-content/uploads/2019/12/1515760522_201.jpg
    https://amitego.ru/wp-content/uploads/2019/12/cinnamon-mint.jpg
    https://amitego.ru/wp-content/uploads/2019/12/Аннотация-2019-12-19-175211.png
    https://amitego.ru/wp-content/uploads/2019/12/translate-1500x10001-1.jpg
    https://amitego.ru/wp-content/uploads/2019/12/Без-названия-5.jpeg
    https://amitego.ru/wp-content/uploads/2019/12/Аннотация-2019-12-06-180411.png
    https://amitego.ru/wp-content/uploads/2019/12/Annotatsiya-2019-12-06-080333.png
    https://amitego.ru/wp-content/uploads/2019/12/desktop.jpg
    https://amitego.ru/wp-content/uploads/2019/11/razrabotchik_cms_bitrix.jpg
    https://amitego.ru/wp-content/uploads/2019/11/Annotatsiya-2019-11-20-222014.png
    https://amitego.ru/wp-content/uploads/2019/11/k1gegUnLhZZTK-oOdPTEPDQUuymB8o_BIBF7-vWNaImHt4Q9kVLqtU0U8HYotElRtQh9001.png
    https://amitego.ru/wp-content/uploads/2019/11/orig.png
    https://amitego.ru/wp-content/uploads/2019/11/1449846994_maxresdefault.jpg
    https://amitego.ru/wp-content/uploads/2019/11/Bez-nazvaniya.png
    https://amitego.ru/wp-content/uploads/2019/10/image-10.png
    https://amitego.ru/wp-content/uploads/2019/09/Без-названия-2.jpg
    https://amitego.ru/wp-content/uploads/2019/09/image-4-1.jpeg
    https://amitego.ru/wp-content/uploads/2019/09/Без-названия-1.jpg
    https://amitego.ru/wp-content/uploads/2019/09/2.png
    https://amitego.ru/wp-content/uploads/2019/09/1.png
    https://amitego.ru/wp-content/uploads/2019/08/linux-desktops-709x381.jpg
    
    • 0

相关问题

  • Android Studio 如何使用 adb 在启动时转到所需的步骤

  • 在 setup.py 中安装依赖项

  • 代码不起作用

  • 是否可以在 TeleBot 中编辑用户的消息?

  • Pyinstaller 中的交叉编译

  • 如何以(dd.mm)格式获取今天的日期?

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    如何从列表中打印最大元素(str 类型)的长度?

    • 2 个回答
  • Marko Smith

    如何在 PyQT5 中清除 QFrame 的内容

    • 1 个回答
  • Marko Smith

    如何将具有特定字符的字符串拆分为两个不同的列表?

    • 2 个回答
  • Marko Smith

    导航栏活动元素

    • 1 个回答
  • Marko Smith

    是否可以将文本放入数组中?[关闭]

    • 1 个回答
  • Marko Smith

    如何一次用多个分隔符拆分字符串?

    • 1 个回答
  • Marko Smith

    如何通过 ClassPath 创建 InputStream?

    • 2 个回答
  • Marko Smith

    在一个查询中连接多个表

    • 1 个回答
  • Marko Smith

    对列表列表中的所有值求和

    • 3 个回答
  • Marko Smith

    如何对齐 string.Format 中的列?

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5