2012-03-21 6 views
1

django에서 lxml.etree.parsetree.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") 

올바른 방향으로 도움을 청할 수 있습니다.

답변

0

아마도 네임 스페이스에 관한 문제 일 수 있습니다. lxml 모듈은 태그 이름의 시작 부분에 네임 스페이스 이름을 추가하기 때문에 xpath 표현식이이 네임 스페이스 접두어와 일치하지 않는 것이 문제 일 수 있습니다. 해당 접두어 "{http://www.w3.org/2005/Atom}"태그 명 앞에 "저자"밖으로

>>> for element in tree: 
...  element 
[...] 
<Element {http://www.w3.org/2005/Atom}author at 7f14e75d1788> 
[...] 

확인 : 당신이 태그 이름을보고 요소를 반복하고,이 같은 것을 얻을 경우,이 문제입니다. 그렇다면, 여기 봐 여기

Need Help using XPath in ElementTree 를 실행 한 다음, 또한

python: xml.etree.ElementTree, removing "namespaces"

그리고 어쩌면 네임 스페이스 접두사없이 구문 분석에 대한 옵션이 있기 때문에 공식 문서를 확인하십시오.

GL.

+0

잘 모르겠지만 아무 것도 할 수 없었습니다. 몇 가지 코드 예제의 변형을 시도했지만 성공하지는 못했습니다. 나는 namespace = "{http://www.w3.org/2005/Atom}" listing = tree.findall ('{% s} author /'% namespace}'을 추가하고'listings = tree '를 제거했습니다. xpath ("// author")'하지만 같은 결과로 끝나는 것처럼 보입니다. ( – bigmike7801

+0

네임 스페이스가 문제 였지만 해결책은 내가 찾던 내용이 아니 었습니다. – bigmike7801

관련 문제