django에서 lxml.etree.parse
및 tree.xpath
을 사용하여 외부 rss 피드의 일부 내용을 구문 분석하려고합니다. 그러나 어떤 이유로 든 결과를 얻을 수 없습니다. 나는 다른 XML 파일에서 성공하기 전에 아래의 방법을 사용할 수 있었지만이 방법으로 어려움을 겪고있는 것처럼 보입니다. 나는 또한 몇 가지를 시도lxml.etree.parse 및 tree.xpath를 사용하여 django에서 결과를 얻지 못하는 이유는 무엇입니까?
def openauthors(request):
tree = lxml.etree.parse("http://www.somedomain.org/people/atom/author_name")
listings = tree.xpath("//author")
listings_info = []
for listing in listings:
this_value = {
"name":listing.findtext("name"),
}
listings_info.append(this_value)
json_listings = '{"listings":' + simplejson.dumps(listings_info) + '}'
if("callback" in request.GET.keys()):
callback = request.GET["callback"]
else:
callback = None
if(callback):
response = HttpResponse("%s(%s)" % (
callback,
simplejson.dumps(listings_info)
), mimetype="application/json"
)
else:
response = HttpResponse(json_listings, mimetype="application/json")
return response
:
여기<feed xmlns="http://www.w3.org/2005/Atom">
<title>Open Library : Author Name</title>
<link href="http://www.somedomain.org/people/atom/author_name" rel="self"/>
<updated>2012-03-20T16:41:00Z</updated>
<author>
<name>somedomain.org</name>
</author>
<id>tag:somedomain.org,2007:/person_feed/123456</id>
<entry>
<link href="http://www.somedomain.org/roll_call/show/1234" rel="alternate"/>
<id>
tag:somedomain.org,2012-03-20:/roll_call_vote/1234
</id>
<updated>2012-03-20T16:41:00Z</updated>
<title>Once upon a time</title>
<content type="html">
This os a book full of words
</content>
</entry>
</feed>
이 같은 장고에 내보기는 모습입니다 : 여기
는 XML 파일 난에서 긁어하려고하는 모습입니다 다음 경로들은 도움을 줄 수는 있지만 성공하지 못했기를 희망합니다.listings = tree.xpath("feed/author")
listings = tree.xpath("/feed/author")
listings = tree.xpath("/author")
listings = tree.xpath("author")
올바른 방향으로 도움을 청할 수 있습니다.
잘 모르겠지만 아무 것도 할 수 없었습니다. 몇 가지 코드 예제의 변형을 시도했지만 성공하지는 못했습니다. 나는 namespace = "{http://www.w3.org/2005/Atom}" listing = tree.findall ('{% s} author /'% namespace}'을 추가하고'listings = tree '를 제거했습니다. xpath ("// author")'하지만 같은 결과로 끝나는 것처럼 보입니다. ( – bigmike7801
네임 스페이스가 문제 였지만 해결책은 내가 찾던 내용이 아니 었습니다. – bigmike7801