You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Beautiful Soup按GUID提取链接并修改isPermalink值?

Hey there! Let's break down how to handle both extracting matching links and modifying GUID attributes using Beautiful Soup—super straightforward once you know the steps.

First, let's parse your RSS feed and pull out links that match your 15 target GUID values or <a> tag IDs. Here's a step-by-step implementation:

Step 1: Install Required Tools

Make sure you have Beautiful Soup and requests (for fetching the RSS feed from a URL) installed:

pip install beautifulsoup4 requests
from bs4 import BeautifulSoup
import requests

# Replace these with your actual 15 GUIDs and IDs
target_guids = {"pubmed:32475840", "pubmed:12345678", "..."}  # Add all 15 GUIDs here
target_ids = {"32475840", "12345678", "..."}  # Add all 15 target IDs here

# Fetch your RSS feed (replace with your feed URL, or read from a local file)
rss_url = "your_rss_feed_url_here"
response = requests.get(rss_url)
# Use the XML parser since RSS is an XML format
soup = BeautifulSoup(response.content, "xml")

matching_links = []

# Loop through each item in the RSS feed
for item in soup.find_all("item"):
    # Check if the item's GUID matches any target
    guid_tag = item.find("guid")
    if guid_tag and guid_tag.text.strip() in target_guids:
        # Extract the link (adjust based on your feed's structure)
        link = item.find("link").text.strip() if item.find("link") else None
        if link:
            matching_links.append(link)
        continue  # Skip checking <a> tag if GUID matches
    
    # Check if any <a> tag in the item has a matching ID
    a_tag = item.find("a")
    if a_tag and a_tag.get("id") in target_ids:
        link = a_tag.get("href")
        if link:
            matching_links.append(link)

# Output the results
print("Matching links found:")
for link in matching_links:
    print(link)

Key Notes:

  • Using the xml parser ensures proper handling of RSS-specific tags and attributes.
  • Adjust the link extraction logic to match your feed's structure—some feeds use a top-level <link> per item, others embed links in <a> tags.
  • Using sets for target_guids and target_ids makes lookups fast (O(1) instead of O(n)).

Absolutely! You can easily update this attribute using Beautiful Soup. Here's how to do it in the same parsing session:

# Find all <guid> tags where isPermalink is set to "false"
for guid_tag in soup.find_all("guid", attrs={"ispermalink": "false"}):
    # Update the attribute value to "true"
    guid_tag["ispermalink"] = "true"

# Save the modified RSS feed to a file (optional)
with open("modified_rss_feed.xml", "w") as f:
    f.write(str(soup))

Explanation:

  • soup.find_all("guid", attrs={"ispermalink": "false"}) targets exactly the GUID tags you want to modify.
  • Setting guid_tag["ispermalink"] = "true" directly updates the attribute in the Beautiful Soup object.
  • Converting the soup to a string (str(soup)) gives you the modified XML content, which you can save or use elsewhere.

Quick Troubleshooting Tips

  • If your feed uses camelCase for the attribute (e.g., isPermalink instead of ispermalink), adjust the attribute name in the attrs dictionary to match exactly.
  • Add null checks (like if guid_tag) to avoid AttributeError if some items lack GUID or <a> tags.

内容的提问来源于stack exchange,提问作者morelloking

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 11:27:27