If I have a HTML Snippet as below, how do I get the required output as below in python.
<td width="10" class="data1"><a class="datalink" href="m01_detail.asp?key=002396653&itemNumber=0">></a></td>
<td class="data1"><a class="datalink" href="m01_detail.asp?key=002396653&itemNumber=0">002396653</a></td>
<td class="data1">IMPORT EXPRESS RECYCLE</td>
<td class="data1">961879066</td>
<td class="data1">11/23/2016</td>
<td class="data1"></td> <!--SARA-->
<td class="data1" align="center">CN</td>
<td class="data1" align="center">PVG</td>
961879066|CN
def reading():
with open("C:\\Users\\John\\Desktop\\test.txt") as f:
for lines in f.readlines():
line = lines.replace("\t","").strip()
print (line)
f.close()
reading()
Thanks,
You can try below code to get required output:
import lxml.html
html = lxml.html.fromstring("""<td width="10" class="data1"><a class="datalink" href="m01_detail.asp?key=002396653&itemNumber=0">></a></td>
<td class="data1"><a class="datalink" href="m01_detail.asp?key=002396653&itemNumber=0">002396653</a></td>
<td class="data1">IMPORT EXPRESS RECYCLE</td>
<td class="data1">961879066</td>
<td class="data1">11/23/2016</td>
<td class="data1"></td> <!--SARA-->
<td class="data1" align="center">CN</td>
<td class="data1" align="center">PVG</td>""")
output = html.xpath('concat(//td[4], "|", //td[7])')
print(output) # '961879066|CN'
Pass original HTML
code to html
variable
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.