2011-03-22 2 views
7

파이썬 스쿠버를 사용하여 본문의 텍스트 만 긁어 내려고했지만 아직 행운을 얻지 못했습니다.스킨 본문 전용

일부 학자가 여기 <body> 태그의 모든 텍스트를 긁어내는 데 도움이되기를 바랄 수도 있습니다.

답변

4

Scrapy는 XPath 표기법을 사용하여 HTML 문서의 일부를 추출합니다. 따라서 /html/body 경로를 사용하여 <body>을 추출해 보셨습니까? (<html>에 중첩되어 있다고 가정). 당신은 Scrapy이 here 제공하는 선택기에 대한 자세한 정보를 찾을 수 있습니다

x.select("//body").extract() # extract body 

: //body 선택기를 사용하는 것이 더 간단 할 수 있습니다.

+0

감사 엘리, 나는 그 부분을 알고 있습니다. 하지만 내 질문은 html 대신 일반 텍스트를 가져 오는 것과 관련이 있습니다. 당신이 알고있는 치료법에 어떤 방법이 있습니까? – mmrs151

+0

@ mmrs151 : 선택자에'/ text()'를 추가하십시오. –

+1

/text()를 추가하면 // body()를 사용하여 본문의 텍스트를 가져오고 body의 모든 하위 요소의 텍스트를 가져옵니다. 그러나 이러한 요소 중 일부는 스크립트 태그와 같이 바람직하지 않은 요소를 포함합니다. – spazm

2

lynx -nolist -dump으로 생성 된 출력을 얻으면 페이지가 렌더링 된 다음 표시되는 텍스트가 덤프됩니다. 저는 단락 요소의 모든 자식들의 텍스트를 추출하여 닫았습니다.

몸 안의 모든 텍스트 요소를 가져온 //body//text()으로 시작했지만 여기에는 스크립트 요소가 포함되었습니다. //body//p은 태그가없는 텍스트 주위의 묵시적인 단락 태그를 포함하여 본문 안의 모든 단락 요소를 가져옵니다. //body//p/text()으로 텍스트를 추출하면 굵게, 기울임 꼴, span, div와 같은 하위 태그의 요소가 누락됩니다. //body//p//text()은 페이지에 스크립트 태그가 단락에 포함되어 있지 않은 한 원하는 콘텐츠를 대부분 얻는 것으로 보입니다.

(XPath /)은 직계 하위를 의미하고 //은 모든 자손을 포함합니다.

% scrapy shell 
In[1]: fetch('http://stackoverflow.com/questions/5390133/scrapy-body-text-only') 
In[2]: hxs.select('//body//p//text()').extract() 

Out[2]: 
[u"I am trying to scrape the text only from body using python Scrapy, but haven't had any luck yet.", 
u'Wishing some scholars might be able to help me here scraping all the text from the ', 
u'&lt;body&gt;', 
u' tag.', 
u'Thank you in advance for your time.', 
u'Scrapy uses XPath notation to extract parts of a HTML document. So, have you tried just using the ', 
u'/html/body', 
u' path to extract ', 
u'&lt;body&gt;', 
u"? (assuming it's nested in ", 
u'&lt;html&gt;', 
u'). It might be even simpler to use the ', 
u'//body', 
u' selector:', 
u'You can find more information about the selectors Scrapy provides ', 
u'here', 

은 공간과 함께 문자열을 가입하고 당신은 꽤 좋은 출력이 있습니다

In [43]: ' '.join(hxs.select("//body//p//text()").extract()) 
Out[43]: u"I am trying to scrape the text only from body using python Scrapy, but haven't had any luck yet. Wishing some scholars might be able to help me here scraping all the text from the &lt;body&gt; tag. Thank you in advance for your time. Scrapy uses XPath notation to extract parts of a HTML document. So, have you tried just using the /html/body path to extract &lt;body&gt; ? (assuming it's nested in &lt;html&gt;). It might be even simpler to use the //body selector: You can find more information about the selectors Scrapy provides here . This is a collaboratively edited question and answer site for professional and enthusiast programmers . It's 100% free, no registration required. about \xbb \xa0\xa0\xa0 faq \xbb \r\n    tagged asked 1 year ago viewed 280 times active 1 year ago"