하얀설표 블로그




해결)Cannot insert a tag into itself./Cannot insert None into a tag.





( 수정됨)


에러 예시

ValueError: Cannot insert a tag into itself.

# Cannot insert a tag into itself.
from bs4 import BeautifulSoup
html = '<p>하얀설표 블로그</p>'
soup: BeautifulSoup = BeautifulSoup(html, 'html.parser')
print(soup)
p = soup.select_one('p')
# soup.append(soup.p)
# print(soup)
p.append(p)

>> <p>하얀설표 블로그</p>
Traceback (most recent call last):
  File "c:\seolpyo\example.py", line 8, in <module>
    p.append(p)
  File "C:\seolpyo\Lib\site-packages\bs4\element.py", line 493, in append
    self.insert(len(self.contents), tag)
  File "C:\seolpyo\Lib\site-packages\bs4\element.py", line 418, in insert
    raise ValueError("Cannot insert a tag into itself.")
ValueError: Cannot insert a tag into itself.

ValueError: Cannot insert None into a tag.

# Cannot insert None into a tag.
from bs4 import BeautifulSoup
html = '<p>하얀설표 블로그</p>'
soup: BeautifulSoup = BeautifulSoup(html, 'html.parser')
print(soup)
p = soup.select_one('p')
p.append(None)

>> <p>하얀설표 블로그</p>
Traceback (most recent call last):
  File "c:\seolpyo\example.py", line 6, in <module>
    p.append(None)
  File "C:\seolpyo\Lib\site-packages\bs4\element.py", line 493, in append
    self.insert(len(self.contents), tag)
  File "C:\seolpyo\Lib\site-packages\bs4\element.py", line 416, in insert
    raise ValueError("Cannot insert None into a tag.")
ValueError: Cannot insert None into a tag.

해결방법

ValueError: Cannot insert a tag into itself.

  1. 태그를 복사하여 기존 beautifulsoup 객체에 추가한다.
  2. 새로운 태그를 생성하여 추가한다.

ValueError: Cannot insert None into a tag.

beautifulsoup 객체는 None 객체를 인정하지 않는다.
태그를 집어넣도록 설정한 것이 맞는지 확인한다.

설명

파이썬 beautifulsoup의 insert나 append 명령은 특정 태그를 선택한 다음, 그 태그 하위에 새로운 태그를 추가하는 명령이다.
자기 자신이 자신의 하위로 들어가는 것은 말이 되지 않는 상황이기 때문에 에러가 발생하게 된다.

None 객체를 집어넣는 경우 역시 제대로 된 태그가 아니기 때문에 에러가 발생한다.

태그를 복사하여 집어넣기

.__copy__() 명령을 통해 태그를 복사하는 것이 가능하다.
이렇게 복사된 태그는 기존 태그와는 다른 객체이기 때문에 동일한 태그지만 기존 태그의 하위로 집어넣는 것이 가능하다.

# 태그 복사
from bs4 import BeautifulSoup
html = '<p>하얀설표 블로그</p>'
soup: BeautifulSoup = BeautifulSoup(html, 'html.parser')
print(soup)
p = soup.select_one('p')
tag_new = p.__copy__()
p.append(tag_new)
print(soup)

>> <p>하얀설표 블로그</p>
<p>하얀설표 블로그<p>하얀설표 블로그</p></p>

새로운 태그를 생성하여 집어넣기

soup 객체가 만들어졌다면, .new_tag() 명령을 통해 새로운 태그를 생성하는 것이 가능하다.
만약 태그의 속성을 추가하고 싶다면 딕셔너리 객체에 key와 value를 추가하는 것처럼 추가하는 것이 가능하다.

태그 안에 들어갈 텍스트를 변경하고 싶다면 .string을 지정해주면 된다.

# 새로운 태그 생성
from bs4 import BeautifulSoup
html = '<p>하얀설표 블로그</p>'
soup: BeautifulSoup = BeautifulSoup(html, 'html.parser')
print(soup)
p = soup.select_one('p')
tag_new = soup.new_tag('p')
tag_new['class'] = 'string'
tag_new.string = p.string
p.append(tag_new)
print(soup)

>> <p>하얀설표 블로그</p>
<p>하얀설표 블로그<p class="string">하얀설표 블로그</p></p>

.text를 사용하지 않는 이유

beautifulsoup 객체는 .text 명령을 통해 태그 안에 있는 문자만을 불러오는 것이 가능하다.
그러나 .text 값을 직접 수정할 수는 없는데, 이는 .text 명령이 어트리뷰트가 아닌 프로퍼티이기 때문이다.
자세한 내용은 이 글에서 확인할 수 있다.

만약 .text값을 직접 수정하려고 한다면 다음과 같은 에러가 발생하게 된다.

# .text
from bs4 import BeautifulSoup
html = '<p>하얀설표 블로그</p>'
soup: BeautifulSoup = BeautifulSoup(html, 'html.parser')
print(soup)
p = soup.select_one('p')
tag_new = soup.new_tag('p')
print(tag_new)
tag_new['class'] = 'string'
print(tag_new)
tag_new.string = p.text
print(tag_new)
tag_new.text = '1234'
print(tag_new)
p.append(tag_new)
print(soup)

>> <p>하얀설표 블로그</p>
<p></p>
<p class="string"></p>
<p class="string">하얀설표 블로그</p>
Traceback (most recent call last):
  File "c:\seolpyo\example.py", line 12, in <module>
    tag_new.text = '1234'
    ^^^^^^^^^^^^
AttributeError: property 'text' of 'Tag' object has no setter


공감 : 0







white.seolpyo.com