【问题标题】:Why I can't scrape all the content within 'data-src' attribute of this HTML为什么我无法抓取此 HTML 的“data-src”属性中的所有内容
【发布时间】:2019-06-12 14:01:59
【问题描述】:

我正在尝试抓取此 html 文本的“data-src”元素中的所有数据:

[<div class="js-delayed-image-load" data-alt="A man covers his face during a sandstorm in Cairo, Egypt, 16 January 2019" data-height="549" data-src="https://ichef.bbci.co.uk/news/320/cpsprodpb/5DE9/production/_105214042_hi051682579.jpg" data-width="976"></div>,

, , , , , , , , , , , , , , , , , , , , ]

我正在使用此代码:

image_containers = soup.find_all('div', class_ = 'js-delayed-image-load')
print(type(image_containers))
print(len(image_containers))

for image in image_containers:
    image.div['data-src']

它给了我这个错误:

TypeError                                 
Traceback (most recent call last)
<ipython-input-546-fa82366c888d> in <module>()
  4 image_containers
  5 for image in image_containers:
 ----> 6     image.div['data-src']

TypeError: 'NoneType' object is not subscriptable

为什么它给我None?谁能告诉我我做错了什么?

谢谢!

【问题讨论】:

    标签: python-3.x web-scraping beautifulsoup html-parsing


    【解决方案1】:

    image 已经是目标div 节点。您不需要再次提取div(它没有子div 所以image.div 返回None)。试试

    for image in image_containers:
        image['data-src']
    

    【讨论】:

    • 成功了吗? Anderson 关于不必再次包含 div 标签是正确的。但是您是否正确解析了您想要的项目?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-22
    • 2013-02-25
    • 2015-11-11
    • 2018-12-16
    • 1970-01-01
    • 2014-01-04
    相关资源
    最近更新 更多