网络抓取python中的xml页面?

时间:2021-01-20 19:00:59

标签: python web-scraping

我很困惑如何从给定的 xml 页面中刮取所有链接(仅包含字符串“mp3”)。以下代码只返回空括号:

# Import required modules 
from lxml import html 
import requests 
  
# Request the page 
page = requests.get('https://feeds.megaphone.fm/darknetdiaries') 
  
# Parsing the page 
# (We need to use page.content rather than  
# page.text because html.fromstring implicitly 
# expects bytes as input.) 
tree = html.fromstring(page.content)   
  
# Get element using XPath 
buyers = tree.xpath('//enclosure[@url="mp3"]/text()') 
print(buyers)

我使用@url 错了吗?

我要找的链接:

enter image description here

任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:2)

会发生什么?

以下 xpath 不起作用,正如您提到的,它使用了 @urltext()

//enclosure[@url="mp3"]/text()

解决方案

任何url中的属性//enclosure都应该包含mp3,然后返回/@url

更改这一行:

buyers = tree.xpath('//enclosure[@url="mp3"]/text()') 

buyers = tree.xpath('//enclosure[contains(@url,"mp3")]/@url') 

输出

['https://www.podtrac.com/pts/redirect.mp3/traffic.megaphone.fm/ADV9231072845.mp3?updated=1610644901',
 'https://www.podtrac.com/pts/redirect.mp3/traffic.megaphone.fm/ADV2643452814.mp3?updated=1609788944',
 'https://www.podtrac.com/pts/redirect.mp3/traffic.megaphone.fm/ADV5381316822.mp3?updated=1607279433',
 'https://www.podtrac.com/pts/redirect.mp3/traffic.megaphone.fm/ADV9145504181.mp3?updated=1607280708',
 'https://www.podtrac.com/pts/redirect.mp3/traffic.megaphone.fm/ADV4345070838.mp3?updated=1606110384',
 'https://www.podtrac.com/pts/redirect.mp3/traffic.megaphone.fm/ADV8112097820.mp3?updated=1604866665',
 'https://www.podtrac.com/pts/redirect.mp3/traffic.megaphone.fm/ADV2164178070.mp3?updated=1603781321',
 'https://www.podtrac.com/pts/redirect.mp3/traffic.megaphone.fm/ADV1107638673.mp3?updated=1610220449',
...]

答案 1 :(得分:1)

它不会直接回答您的问题,但您可以查看 BeautifulSoup 作为替代方案(并且它也可以选择在箍下使用 lxml)。

import lxml # early failure if not installed
from bs4 import BeautifulSoup
import requests 
  
# Request the page 
page = requests.get('https://feeds.megaphone.fm/darknetdiaries') 

# Parse
soup = BeautifulSoup(page.text, 'lxml')

# Find
#mp3 = [link['href'] for link in soup.find_all('a') if 'mp3' in link['href']]
# UPDATE - correct tag and attribute
mp3 = [link['url'] for link in soup.find_all('enclosure') if 'mp3' in link['url']]