正则表达式的使用疑惑

部分代码如下#

                                <tr>
                                    <th>ID</th>
                                    <th>Key Name</th>
                                    <th>Value</th>
                                    <th>Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr id="cancel">
   <td>1</td>
   <td>cancel</td>
   <td id="edit_cancel">Cancel</td>
   <td>
      <button type="button" class="btn btn-default waves-effect btn-lang m-r-20" data-id="cancel" data-toggle="modal" data-target="#defaultModal">EDIT</button>
   </td>
</tr><tr id="delete">
   <td>2</td>
   <td>delete</td>
   <td id="edit_delete">Delete</td>
   <td>
      <button type="button" class="btn btn-default waves-effect btn-lang m-r-20" data-id="delete" data-toggle="modal" data-target="#defaultModal">EDIT</button>
   </td>
</tr><tr id="my_profile">
   <td>3</td>
   <td>my_profile</td>
   <td id="edit_my_profile">My Profile</td>
   <td>
      <button type="button" class="btn btn-default waves-effect btn-lang m-r-20" data-id="my_profile" data-toggle="modal" data-target="#defaultModal">EDIT</button>
   </td>

Q:#

  • 如何把 html 中的 VALUE 值提取出来。
    eg:将 Cancel 提取并存储。
    <td id="edit_cancel">Cancel</td>
  • 将存储起来的字符访问有道翻译进行翻译,将翻译的结果进行可读写存储。
讨论数量: 2

这种 html 格式的建议你用 Xpath,我给你打个样

from lxml import etree
html = """                                <tr>
                                    <th>ID</th>
                                    <th>Key Name</th>
                                    <th>Value</th>
                                    <th>Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr id="cancel">
   <td>1</td>
   <td>cancel</td>
   <td id="edit_cancel">Cancel</td>
   <td>
      <button type="button" class="btn btn-default waves-effect btn-lang m-r-20" data-id="cancel" data-toggle="modal" data-target="#defaultModal">EDIT</button>
   </td>
</tr><tr id="delete">
   <td>2</td>
   <td>delete</td>
   <td id="edit_delete">Delete</td>
   <td>
      <button type="button" class="btn btn-default waves-effect btn-lang m-r-20" data-id="delete" data-toggle="modal" data-target="#defaultModal">EDIT</button>
   </td>
</tr><tr id="my_profile">
   <td>3</td>
   <td>my_profile</td>
   <td id="edit_my_profile">My Profile</td>
   <td>
      <button type="button" class="btn btn-default waves-effect btn-lang m-r-20" data-id="my_profile" data-toggle="modal" data-target="#defaultModal">EDIT</button>
   </td>"""
select = etree.HTML(html)
print(select.xpath('//td[@id="edit_cancel"]/text()')[0])

详细的 Xpath 用法你可以看我这篇文章

3年前 评论
Jason990420

Example here,

代码已被折叠,点此展开
1,cancel,Cancel,button ==> 1,cancel,取消,button
2,delete,Delete,button ==> 2,delete,删除,button
3,my_profile,My Profile,button ==> 3,my_profile,我的资料,button
3年前 评论