2014-10-20 1 views
0

_Comment의 내용을 가져 오려고합니다. 나는 그것을 어떻게하는지에 대해 상당히 연구했지만, 텍스트를 얻기 위해 td 엘리먼트에서 함수에 접근하는 방법을 모른다. 도움이된다면 xpaths를 Python Scrapy 모듈과 함께 사용하고 있습니다.lxml에서 텍스트 가져 오기 Comment

td = None [_Element] 
    <built-in function Comment> = None [_Comment] 
    a = None [_Element] 

td 요소의 HTML은 다음과 같습니다

<table class="crIFrameReviewList"> 

    <tr> 
     <td> 

<!-- BOUNDARY --> 
<a name="R2L4AFEICL8GG6"></a><br /> 


<div style="margin-left:0.5em;"> 

     <div style="margin-bottom:0.5em;"> 
     304 of 309 people found the following review helpful 
     </div> 
     <div style="margin-bottom:0.5em;"> 
     <span style='margin-left: -5px;'><img src="http://g-ecx.images-amazon.com/images/G/01/x-locale/common/customer-reviews/stars-5-0._V192240867_.gif" width="64" alt="5.0 out of 5 stars" title="5.0 out of 5 stars" height="12" border="0" /> </span> 
     <b>Great Travel Zoom</b>, <nobr>April 9, 2014</nobr> 
     </div> 
     <div style="margin-bottom:0.5em;"> 

     <div class="tiny" style="margin-bottom:0.5em;"> 
     <span class="crVerifiedStripe"><b class="h3color tiny" style="margin-right: 0.5em;">Verified Purchase</b><span class="tiny verifyWhatsThis">(<a href="http://www.amazon.com/gp/community-help/amazon-verified-purchase" target="AmazonHelp" onclick="amz_js_PopWin('http://www.amazon.com/gp/community-help/amazon-verified-purchase', 'AmazonHelp', 'width=400,height=500,resizable=1,scrollbars=1,toolbar=0,status=1');return false; ">What's this?</a>)</span></span> 
     </div> 
     <div class="tiny" style="margin-bottom:0.5em;"> 
     <b><span class="h3color tiny">This review is from: </span>Canon PowerShot SX700 HS Digital Camera (Black) (Electronics)</b> 
     </div> 

For the recent few years Canon has made great efforts to improve their travel-zoom compact cameras, and the new SX700 is their next remarkable achievement on that way. It's a little bit bigger than its predecessor (SX280) but it is very well built and has an attractive look and feel (I like the black one). It also got a new front grip which makes one-hand shooting more convenient, even when shooting video, since the Video button was moved from the back to the top and you can now use your thumb solely for holding the camera.<br /><br />Here is a brief list of the new camera pros & cons:<br /><br />PROS:<br />* A very good design and build quality with the attractive finish.<br />* A new powerful 30x optical zoom lens in just a pocket-size body.<br />* Incredible range from 25mm wide to 750mm telephoto for stills and video.<br />* Zoom Framing Assist - very useful new feature to compose your pictures at long telephoto.<br />* Very effective optical Intelligent Image Stabilization for... 


<a href="http://rads.stackoverflow.com/amzn/click/B00I58M26Y" target="_top">Read more</a> 
     <div style="padding-top: 10px; clear: both; width: 100%;"> 
+0

처리중인 실제 HTML을 표시 할 수 있습니까? 또한 원하는 출력이 도움이됩니다. – alecxe

+0

@alecxe 편집 내용으로 업데이트했습니다. html을 lxml.etree.HTML 객체로 변환했습니다. – robert

+0

'class = "reviewText"'가있는'div' 요소가 여러 개 있습니까? 그렇지 않으면'xpath'로''// div [@ class = "reviewText"]/text() ''를 사용할 수 있습니다. –

답변

1

은 찾기 divtext 방법 tostring()를 사용하여 문자열 요소 .//div[@class="reviewText"] XPath 식을 사용하여 덤프 class="reviewText"와 :

import lxml.html 

data = """ 
your html here 
""" 

td = lxml.html.fromstring(data) 
review = td.find('.//div[@class="reviewText"]') 
print lxml.html.tostring(review, method="text") 

인쇄물 :

54,000 RPM - It has a spinning disk drive that is way beyond our time...I bought 10 of these just for the hard drive, they blow SSD's out of the water. 
Seriously though... how does a well known computer company mistype an important spec? 
+0

아마존에서 나에게 주어진 것을 갖도록 편집했습니다. 그들은 자신의 사이트에 대한 반 긁기 기술을 구현하고 xpaths에서의 경험이 컸다고 생각하지만,이 것은 나를 괴롭 히고 있습니다. 가장 가까운 것은''입니다. 내가 접근 할 수만 있다면. – robert

+0

@robert ok, 나와 함께 시작할 수있는 현재 코드를 추가 할 수 있습니까? 감사. – alecxe