ฉันประสบปัญหาในการแคร็กอันนี้ ฉันมีฟีด RSS ในรูปแบบไฟล์ XML อย่างง่ายดูเหมือนว่านี้:
<rss version="2.0">
<channel>
<title>My RSS Feed</title>
<link href="https://www.examplefeedurl.com">Feed</link>
<description></description>
<item>...</item>
<item>...</item>
<item>...</item>
<item>
<guid></guid>
<pubDate></pubDate>
<author/>
<title>Title of the item</title>
<link href="https://example.com" rel="alternate" type="text/html"/>
<description>
<![CDATA[<a href="https://example.com" target="_blank" rel="noopener noreferrer">View Example</a>]]>
</description>
<description>
<![CDATA[<p>This actually contains a bunch of text I want to work with. If this text contains certain strings, I want to get rid of the whole item.</p>]]>
</description>
</item>
<item>...</item>
</channel>
</rss>
วัตถุประสงค์ของฉันคือการตรวจสอบว่าแท็กคำอธิบายที่สองมีสตริงบางอย่างหรือไม่ หากมีสตริงนั้นอยู่ ฉันต้องการลบออกทั้งหมด ขณะนี้อยู่ในรหัสของฉันฉันมีสิ่งนี้:
doc = lxml.etree.fromstring(testString)
found = doc.findall('channel/item/description')
for desc in found:
if "FORBIDDENSTRING" in desc.text:
desc.getparent().remove(desc)
และจะลบแท็กคำอธิบายที่สองซึ่งสมเหตุสมผลออก แต่ฉันต้องการให้ รายการ ทั้งหมดหายไป ฉันไม่รู้ว่าจะระงับองค์ประกอบ 'รายการ' ได้อย่างไร หากฉันมีเพียงข้อมูลอ้างอิง 'รายละเอียด'
ฉันได้ลองใช้ Google และค้นหาที่นี่แล้ว แต่สถานการณ์ที่ฉันเห็นเพียงต้องการลบแท็กเหมือนที่ฉันทำอยู่ตอนนี้ น่าแปลกที่ฉันไม่ได้สะดุดกับโค้ดตัวอย่างที่ต้องการกำจัดออบเจ็กต์พาเรนต์ทั้งหมด ยินดีเป็นอย่างยิ่งที่จะให้คำแนะนำเกี่ยวกับเอกสาร/บทช่วยสอนหรือความช่วยเหลือ