简体   繁体   English

Python将字符串索引拆分超出范围

[英]Python splitting string index out of range

I am trying to split the following a tag: 我正在尝试拆分以下标签:

<h3><a href="#AC Adapter" onclick="getProductsBasedOnCategoryID('Asus','AC Adapter','ET1611PUT','6941', this, 'E Series')">AC Adapter

            </a></h3>

Using the following code: 使用以下代码:

print "FETCHING CATEGORY"
    atag = s.h3
    for data in atag:
        while getattr(atag, 'name', None) != 'h3':
            atag = atag.nextSibling
        atag.a
        atag = literal_eval('(' + atag.nextSibling.replace(', this', '').split('(', 1)[1])
        print atag

However, i get the following error: 但是,我得到以下错误:

File "//CPSBS/RedirectedFolders/aysha/My Documents/asus_tables(edited) a tags.py", line 84, in <module>
    atag = literal_eval('(' + atag.nextSibling.replace(', this', '').split('(', 1)[1])
IndexError: list index out of range

I am guessing there is something wrong i am doing? 我猜我在做错什么? Also this a tag has an onclick attribute i would like to access instead so how would i input that into the following code? 另外这a标签具有onclick属性我想访问代替如何将我输入到这下面的代码?

Here is the url i am parsing the data from 这是我正在解析数据的网址

http://www.asusparts.eu/partfinder/Asus/All In One/E Series http://www.asusparts.eu/partfinder/Asus/All In One / E系列

[EDIT] [编辑]

Navigational Tree i am trying to retrieve the data from 我试图从导航树中检索数据

<div id="accordion" class="ui-accordion ui-widget ui-helper-reset ui-accordion-icons" style="width: 760px;" role="tablist"> 
    <h3 class="ui-accordion-header ui-helper-reset ui-state-active ui-corner-top" role="tab" aria-expanded="true" aria-selected="true" tabindex="0"> 
        <span class="ui-icon ui-icon-triangle-1-s"></span> 
        <a onclick="getProductsBasedOnCategoryID('Asus','AC Adapter','ET10B','6941', this, 'E Series')" href="#AC Adapter" tabindex="-1" loaded="Loaded">AC Adapter </a> 
    </h3> 
    <div id="6941" class="ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom ui-accordion-content-active" role="tabpanel" style="display: block;"> 
        <table class="productTableList"> 
            <tbody> 
        </table> 
        <table class="productTableList"> 
            <tbody> 
                <tr style="height:90px;background-color:#ebf4ff;"> 
                    <td class="ProduktLista" width="70px"> 
                    <td class="ProduktLista" width="315"> 
                        <a onclick="getProductInformationModal("Asus","14G110008340");"> 
                        <br>  

When you're faced with these types of problems, and you can't immediately see what the problem is, then you need to divide up the complex expression. 当您面对这些类型的问题,而又无法立即看到问题所在时,则需要对复杂的表达式进行划分。 Instead of: 代替:

atag = literal_eval('(' + atag.nextSibling.replace(', this', '').split('(', 1)[1])

rewrite it to (you should, of course, use variable names that make more sense): 将其重写为(当然,您应该使用更有意义的变量名称):

nextSibling = atag.nextSibling
txt1 = nextSibling.replace(', this', '')
split = txt1.split('(', 1)
txt2 = split[1]
txt3 = '(' + txt2
atag = literal_eval(txt3)

This will get you the exact expression where the problem exist, and a print statement of the values involved should give you the answer.. 这将为您提供存在问题的确切表达式,并且所涉及值的打印语句应为您提供答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM