我有一个实现 BeautifulSoup 的 Python 脚本,它解析同一目录中的 XML 文件。我想用完全相同的 XML 格式解析网站上的很多页面。我想为脚本提供一个 url,它可以在该 URL 处获取页面的 XML 并对其进行解析,而不是下载每个 XML 并更改xml_file
原始代码中的内容。这是我的代码所需要的。我正在尝试xml_file="somefileID.xml"
用获取页面 xml 的脚本替换。
#The program just goes through and pulls info from different tags.
from bs4 import BeautifulSoup
xml_file="somefileID.xml" #get this ID from the page using a script somehow??
#Then somehow put that id you got into "http://someurl.com/"+xml_file
xml_string = open(xml_file).read() #go on to read your new xml file
#Status
soup = BeautifulSoup(xml_string)
status = soup.find('some-tag')['some-attribute']
print "\nSome Prompt: "+attribute+"\n"
print "Most Recent Event Information: \n"
#Most Recent Event Date
event_date = lambda x: x.name == "date"
events = soup.findAll(event_date)
if(events):
# The last event-data
print "Date: "+events[-2].text
print "Analysis Complete."
感谢您的任何想法!