2017-11-10 11 views
0

테이블의 각 행에 대해이 명령을 사용하고 있지만 이탤릭체가 아닌 텍스트 만 가져옵니다.lxml로 기울임 꼴 및 이탤릭체 텍스트를 가져 오는 방법

name = ''.join(row.xpath('td[3]/a/text()')) 

a 요소는 <em> </em 태그에 텍스트를 가지고있다.

<td class="cardname"><a href="http://www.mtgotraders.com/store/PRM_Ball_Lightning_f.html"><em>Ball</em> <em>Lightning</em> *Foil*</a></td> 

나는 Ball Lightning *Foil*

+0

적어도 기울임 꼴 및 비 기울임 꼴 텍스트를 사용할 수있는 몇 가지 요소와 질문을 업데이트합니다. – SIM

답변

0

이 당신이 원하는 무엇인가를 얻으려면? xpath 또는 css selector를 사용하든 결과는 항상 동일합니다. 이 총을 보내기

html_content=''' 
<td class="cardname"><a href="http://www.mtgotraders.com/store/PRM_Ball_Lightning_f.html"> 
<em>Ball</em> <em>Lightning</em> *Foil*</a></td> 
''' 
from lxml.html import fromstring 

root = fromstring(html_content) 
item = root.cssselect(".cardname a")[0].text_content().strip() 
item_alternative = root.xpath("//*[@class='cardname']/a")[0].text_content().strip() 

print(item) 
print(item_alternative) 

결과 :

Ball Lightning *Foil* 
Ball Lightning *Foil*