带括号的Python列表项打印

时间:2016-01-12 00:21:51

标签: python selenium web-scraping

  1. 我用值
  2. 启动了一系列变量
  3. 我把这些变量放在一个列表中
  4. 我尝试在while循环中打印这两个值...

    data [0]和str(data [0])

  5. 使用list []括号打印此数据的问题,如果我在将变量传递到列表之前直接打印变量,则不会发生这种情况 - 我将在函数上为此程序执行此操作

  6. 我的代码:

    # initiate data 
    
        # personal info
            businessName = "//*[@id='BusinessName']"
            firstName = "//*[@id='BusinesOwnersFirstName']"
            lastName = "//*[@id='BusinesOwnersLastName']"
            ubi = "//*[@id='UBINumber']"
            info = "//*[@id='persInfoContainer']"
    
        # liscensing info
            licenseType = "//*[@id='LicenseType']"
            specialties = "//*[@id='layoutContainer']/div[5]/div[6]/label[1]"
            licenseNumber = "//*[@id='LicenseNumber']"
            licenseEffectiveDate = "//*[@id='EffectiveDate']"
            licenseExpirationDate = "//*[@id='ExpirationDate']']/div[5]/div[6]/label[2]"
            status = "//*[@id='StatusDescription']"
    
        # bond info
            bondProvider = "//*[@id='BondsCompanyName']"
            bondNumber = "//*[@id='BondAccountNumber']"
            bondAmount = "//*[@id='BondAmount']"
            bondEffectiveDate = "//*[@id='BondsEffectiveDate']"
            bondExpirationDate = "//*[@id='BondsExpirationDate']" 
    
            # insurance info
            insuranceProvider = "//*[@id='InsurancesFirmName']"
            insuranceNumber = "//*[@id='InsurancesAccountId']"
            insuranceAmount = "//*[@id='InsurancesAmount']"
            insuranceEffectiveDate = "//*[@id='InsurancesEffectiveDate']"
            insuranceExpirationDate = "//*[@id='InsurancesExpirationDate']"
    
    
    # -------------------------------------------------------------------------------------   
    
    # data list
    # -------------------------------------------------------------------------------------   
            data = [
            [businessName],[firstName],[lastName],[ubi],[info],
            [licenseType],[specialties],[licenseNumber],[licenseEffectiveDate],[licenseExpirationDate],[status],
            [bondProvider],[bondNumber],[bondAmount],[bondEffectiveDate],[bondEffectiveDate],[insuranceProvider],
            [insuranceNumber],[insuranceAmount],[insuranceEffectiveDate],[insuranceExpirationDate]
            ] 
    
            # print data
    
    
    # needed logic outlne:
    #     get list 
    #         loop thorugh checking is xpath is available
    #             if not available set dataOut to ""
    #             else get xpathData and set dataOut to xpathData
    #                 export to csv file
    
    
    
    
    # -------------------------------------------------------------------------------------
    
    # xpath check and grab function
    # -------------------------------------------------------------------------------------
    
            i = 0
            l = len(data)-1
            while (i <= l):
                print "string: "+ str(data[0])
                print "non-string: "+ data[0]
                try:
                    # self.browser.find_element_by_xpath(data[i]) #checks is xpath exists
                    # return True
                    result = browser.find_element_by_xpath(data[0]).text
                    print "result"+result
                except:
                    # return False
                    print "ERROR: empty xpath"
                i += 1
    

    我的输出(在命令行中):

    string: ["//*[@id='BusinessName']"]
    print "non-string: "+ data[0]
    TypeError: cannot concatenate 'str' and 'list' objects
    

2 个答案:

答案 0 :(得分:3)

由于某种原因,您将每个字符串放入单个list中,因此(如错误所示)data[0]本身就是list。如果您想要list中包含的字符串,请使用data[0][0]访问它(或者需要任何适当的索引,假设您曾添加到list中的data。您可以使用+,作为此内容。

print "non-string: " + data[0][0]

您也可以通过print分别处理每个对象(使用,而不是+)而不是尝试连接它们来打印对象本身:

print "non-string:", data[0]

请注意,这会导致list被打印,并带有方括号,例如: non-string: ["//*[@id='BusinessName']"]

但是,我建议按照以下方式删除这些单例list(除非您出于某种原因确实需要它们):

data = [
    businessName, firstName, ...

然后data[0]将是一个字符串。

答案 1 :(得分:-1)

如果您尝试打印列表,连接字符串或将元素发送到函数find.element_by_x_path,您的问题有点令人困惑,但我会尽我所能。它还取决于您使用的Python版本(推荐Python 3 :))您将获得TypeError,因为Python无法连接字符串和列表对象。如果您只想打印数据列表,请使用简单的“for”循环

for item in data:
   print (item)

如果您想在上面的while循环中连接,请尝试类似

的内容
for string in data: # Takes list into iterable objects
    newstring = string + "something here"
    print (string)

您还可以使用Python的内置“类型”来帮助您处理变量,以确保传递您想要传递给find.element_by_xpath的内容

type(data)
<<<    <class 'list'>

还有一个索引错误,你正在做的字符串类型列表“[] []”

尝试将输入切换到数据[0] [0]或通过Python的类型命令运行它以查看这是否是您想要的

希望这有帮助!