【问题标题】:Not able to retrieve parent tag using BeautifulSoup and python无法使用 BeautifulSoup 和 python 检索父标签
【发布时间】:2017-02-11 14:12:53
【问题描述】:

我正在尝试从 Instagram 页面检索 img 标签的父元素。但我收到错误:

AttributeError:'NoneType' object has no attribute 'parent'

下面是我的简单代码:

from urllib.request import urlopen
from bs4 import BeautifulSoup

url=urlopen("https://www.instagram.com/people")
html=url.read()
soup=BeautifulSoup(html,"html.parser")
elem=soup.find('img',{'id':'pImage_0'}).parent
print((elem))

谁能告诉我为什么我无法检索父标签或我在做什么错误?我可以检索 img 标签的内容(通过从代码中删除父级):

<img alt="Congrats! ???? #CassadeePope is engaged to #AllTimeLow drummer    #RianDawson! Get all of the sweet details in our bio link! | #Repost @cassadeepope: WE'RE ENGAGED ❤❤❤❤❤❤❤❤❤❤❤❤❤" class="_icyx7" data-reactid="43" id="pImage_0" src="https://instagram.fdel1-1.fna.fbcdn.net/t51.2885-15/s640x640/sh0.08/e35/16465038_376289889403372_5052986327474110464_n.jpg?ig_cache_key=MTQ0NzUzMjA0NDk1NTIzNDkzNA%3D%3D.2" style="visibility:hidden;"/>

【问题讨论】:

    标签: python python-3.x beautifulsoup web-crawler bs4


    【解决方案1】:

    这个页面是用JavaScript加载的,而img标签是用JS生成的,请求只返回HTML代码,所以无法获取HTML代码和

    soup.find('img',{'id':'pImage_0'})
    

    将返回None

    【讨论】:

    • 这不是真的。 soup.find('img',{'id':'pImage_0'})返回了img标签中的内容
    • @Naveen Gabriel 打印出来并发布在问题中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-10
    • 1970-01-01
    • 1970-01-01
    • 2015-03-08
    相关资源
    最近更新 更多