【发布时间】: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