简体   繁体   中英

TypeError: expected string or buffer with BeautifulSoup

I want to remove a few tags from some HTML:

<p class="mt-20" itemprop="description"> and </p> . Everything else, such as <br> should remain.

Code:

from bs4 import BeautifulSoup
import requests
import re
url = 'https://www.tokopedia.com/tokoonline22/sendok-ukur-elektrik-500g-maks-white?'
page3 = requests.get(url)
soup3 = BeautifulSoup(page3.text, "lxml")

#No problem.
#v = """<p class="mt-20" itemprop="description">*OMCB07BK*<br/><br/>Tas backpack ini didesign khusus untuk menaruh drone DJI Phantom 3 beserta dengan aksesoris-aksesorisnya seperti propeller, baterai dan remot kontrol. Setiap slot tas didesign untuk menaruh semua part dari drone DJI sehingga drone mudah dirakit saat ingin digunakan.<br/><br/>Features<br/><br/>Shoulder Bag<br/>Tas ini mirip seperti tas ransel hanya berbeda pada komparmen penyimpanan dimana setiap komparmen didesign untuk menaruh part-part dari DJI Phantom 3.<br/><br/>Easy to Access<br/>Anda dapat merekit dan menggunakan drone dengan sangat cepat dan mudah berkat designnya yang terbuka.<br/><br/>Designed for DJI Phantom 3<br/>Didesign khusus untuk menaruh drone DJI Phantom 3 selain drone juga dapat menaruh aksesoris-aksesoris nya.<br/><br/>Specifications<br/>Dimension    37 x 26 x 7 cm</p></p>
#"""

v = soup3.find("p", {"itemprop": "description"})
result = re.sub('<p class="mt-20" itemprop="description">', "", v)
print(result)

Output error:

result = re.sub('<p class="mt-20" itemprop="description">', "", v)
File "/usr/lib/python2.7/re.py", line 151, in sub
return _compile(pattern, flags).sub(repl, string, count)
TypeError: expected string or buffer

re.sub needs a string variable, your v variable is a bs4 element tag . You can change the bs4 element to string:

newv = str(v)
result = re.sub('<p class="mt-20" itemprop="description">', "", newv)

On the other hand, you don't actually need regular expressions. You can unwrap (this is what I think you are trying to achieve) an element with BeautifulSoup itself:

for elm in soup3.find_all("p", {"itemprop": "description"}):
    elm.unwrap()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM