如何使用Beautiful Soup从HTML中获取文本

时间:2015-10-21 07:30:12

标签: python xpath beautifulsoup

我想知道如何从此HTML中获取文本A1 Pawn

<tr id="overview-summary-current">
<th scope="row">
    <span class="edit-tools">
        <a href="#background-experience" class="edit-section" id="control_gen_4">Edit experience</a>
        <script id="controlinit-dust-server-65573249-4" type="text/javascript+initialized" class="li-control">LI.Controls.addControl("control-dust-server-65573249-4","IntraScroller",{tracking:'top-card-edit-experience',paddingTop:-20})</script>
        <script type="text/javascript">if(dust&&dust.jsControl){if(!dust.jsControl.flushControlIds){dust.jsControl.flushControlIds="";}else{dust.jsControl.flushControlIds+=",";}dust.jsControl.flushControlIds+="control-dust-server-65573249-4";}</script>
    </span>
    <a href="#background-experience" data-trk="prof-0-ovw-curr_pos">Current</a>
</th>
<td>
    <ol>
        <li>
            <span data-tracking="mcp_profile_sum" class="new-miniprofile-container /biz/miniprofile/8241336?pathWildcard=8241336" data-li-url="/biz/miniprofile/8241336?pathWildcard=8241336" data-li-getjs="https://static.licdn.com/scds/concat/common/js?h=40vfeoewuurexnhvi1o2qiknu&amp;fc=2" data-li-miniprofile-id="LI-2326069">
                <strong>
                    <a href="/company/8241336?trk=prof-0-ovw-curr_pos" dir="auto">A1 Pawn</a>
                </strong>
            </span>
        </li>
    </ol>
</td>

我尝试过使用CSS Selector和xpath来获取文本

使用CSS选择器不起作用:

str(profilePageSource.find_element_by_css_selector("#overview-summary-current > td > ol > li > span > strong > a").get_text().encode("utf-8"))[2:-1]

使用Xpath无法正常工作:

str(profilePageSource.find_element_by_xpath("//*[@id=\"overview-summary-current\"]/td/ol/li/span/strong/a").get_text().encode("utf-8"))[2:-1]

3 个答案:

答案 0 :(得分:2)

对于CSS选择器,您应该使用soup.select()方法,而不是namespace WpfItemsControl { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { try { InitializeComponent(); this.DataContext = new DataStore(); } catch (Exception ex) { Debug.WriteLine(ex.InnerException.Message); } } } public class DataStore { public List<string> Names { get; set; } public DataStore() { Names = new List<string>(); Names.Add(">"); Names.Add(">"); Names.Add(">"); Names.Add(">"); Names.Add(">"); } } 。示例 -

.find_element_by_css_selector

演示 -

elems = profilePageSource.select("#overview-summary-current > td > ol > li > span > strong > a")
if elems:
    print(str(elems[0].get_text().encode("utf-8"))[2:-1]))

答案 1 :(得分:1)

soup.find(id='overview-summary-current').td.a.text应该会给你结果。

答案 2 :(得分:0)

你也可以通过以下方式获得

soup.find('a', {'dir': "auto"}).text