2013-06-11 3 views
2

나는 Scrapy를 사용하여 article을 얻고 있습니다.치료법을 사용하여 xpath를 더 선택적으로 만드는 방법은 무엇입니까?

>>> articletext = hxs.select("//span[@id='articleText']") 
>>> for p in articletext.select('.//p'): 
...  print p.extract() 
... 
<p class="byline">By Patricia Reaney</p> 
<p> 
     <span class="location">NEW YORK</span> | 
     <span class="timestamp">Tue Apr 3, 2012 6:19am EDT</span> 
</p> 
<p><span class="articleLocation">NEW YORK</span> (Reuters) - Ba 
track of finances, shopping and searching for jobs are the mai 
et users around the globe, according to a new international 
survey.</p> 
<p>Nearly 60 percent of people in 24 countries used the web to 
account and other financial assets in the past 90 days, making 
ar use of the Internet.</p> 
<p>Shopping was not far behind at 48 percent, the Ipsos poll fo 
and 41 percent went online in search of a job.</p> 
<p>"It is easy. You can do it any time of the day and most of t 
on't have fees," said Keren Gottfried, research manager for Ips 
Affairs, about banking online.</p> 

필자 만 회람, 타임 스탬프 및 기사 위치를 제거하고 기사 만 남기고 싶습니다. 또는 기사를 추출하는 것이 더 좋습니다. 어떻게해야합니까?

답변

0

당신이 시도 할 수이 3 개 <p> 태그를 반환해야이

articletext = hxs.select("//span[@id='articleText']/p[position()>2]") 

: 당신은 아마 그 후 수동으로 articleLocation를 제거해야합니다

<p><span class="articleLocation">NEW YORK</span> (Reuters) - Ba 
track of finances, shopping and searching for jobs are the mai 
et users around the globe, according to a new international 
survey.</p> 

<p>Nearly 60 percent of people in 24 countries used the web to 
account and other financial assets in the past 90 days, making 
ar use of the Internet.</p> 

<p>Shopping was not far behind at 48 percent, the Ipsos poll fo 
and 41 percent went online in search of a job.</p> 
<p>"It is easy. You can do it any time of the day and most of t 
on't have fees," said Keren Gottfried, research manager for Ips 
Affairs, about banking online.</p> 

하지만.

+0

이렇게 할 때 반환되는 값은 "

"입니다. 당신은 하루 중 언제든지 할 수 있고 대부분 에는 요금이 없습니다 "라고 Irene 업무 담당 연구 매니저 인 Keren Gottfried는 온라인 뱅킹에 대해 말했다.

" – Jean

+0

그 이상한 것은 실생활에 해당하는 코드 예제입니다. 그때? 아니면 출력 결과를 테스트하고 있습니까? 그'p'는 제 예제에서는 4 번째지만, 6 번째 샘플은 당신 것입니다. 어떤 다른 일이 일어날 수 있습니다 제안합니다. –

+0

오 코드 예제가 출력되었습니다. 주어진 링크에서 소스 코드를 사용합니다. 귀하의 방법은 작동하지만, 나는 깨달았다. 나는 focusparagraph = hxs.select ("// span [@ class = 'focusParagraph']/p)로 내 문제를 해결했다. articletext = hxs.select ("// span [@ id = 'articleText']/p) focusparagraph P에 대한 문서 = ' : ftext p.extract =() articletext에 대한 P : atext p.extract =() 문서 + = atext 인쇄 ftext + 문서 – Jean

0

글쎄, 조건을 추가하면 그럴 수 있습니다. <p>. 이처럼하려고하면 "클래스가도 아이들이 수업을 가진 요소에 걸쳐이없는 모든 P 요소"를 의미

//span[@id="articletext"]//p[not(@class)][not(span[@class])]

. 둘 이상의 조건을 필터로 사용할 수 있습니다.

관련 문제