【问题标题】:Extract URL in Sitemap With Python使用 Python 提取站点地图中的 URL
【发布时间】:2020-06-26 12:01:01
【问题描述】:

我需要站点地图中的提取链接 https://wunder.com.tr/sitemap.xml

我写了一些代码

import requests
from bs4 import BeautifulSoup

wunder = requests.get("https://wunder.com.tr/sitemap.xml")
parcala = BeautifulSoup(wunder.content,"lxml")

links = parcala.find_all("html-tag")
print(links)

但无法提取。

【问题讨论】:

  • 试试这个,[x.text for x in parcala.find_all("loc")]
  • 那只是因为没有名为html-tag的元素。尝试简单的html,您将获得整个 html。你能具体告诉我你想从这个网站上提取什么吗?

标签: python python-3.x beautifulsoup request


【解决方案1】:
import requests
from bs4 import BeautifulSoup

wunder = requests.get("https://wunder.com.tr/sitemap.xml")
parcala = BeautifulSoup(wunder.content, "xml")

urls_from_xml = []

loc_tags = parcala.find_all('loc')

for loc in loc_tags:
    urls_from_xml.append(loc.get_text()) 
   
print(urls_from_xml)

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 1970-01-01
  • 2019-03-05
  • 2015-02-01
  • 1970-01-01
  • 2023-03-30
  • 1970-01-01
  • 1970-01-01
  • 2011-08-18
  • 1970-01-01
相关资源
最近更新 更多