。split()函数提供IndexError:使用beautifulsoup超出列表索引范围

问题描述 投票:1回答:2

代码:

url = 'https://steamcommunity.com/market/itemordershistogram?country=CN&language=english&currency=23&item_nameid=1321474'
source = requests.get(url, cookies=cookies, headers=headers, timeout=10)
source.encoding = 'utf-8'
if(source.ok != True):
    print(f'\nError: {source.status_code}\n')
    return 0
soup = str(BS(source.text, 'lxml'))
print(soup)
print(type(soup))
steam = []
steam.append(int(soup.split(""""sell_order_summary":"<span class='\"market_commodity_orders_header_promote\"'>""")[1].split(' for sale')[0])) # Sell Quantity
print(steam)

错误:

Traceback (most recent call last):
  File "C:\Users\Andrew\Desktop\ultralight\REQUESTS_MODULE.py", line 188, in <module>
    print(request_buff_steam_item('https://steamcommunity.com/market/listings/730/Desert%20Eagle%20%7C%20Blaze%20%28Factory%20New%29'))
  File "C:\Users\Andrew\Desktop\ultralight\REQUESTS_MODULE.py", line 182, in request_buff_steam_item
    steam.append(int(soup.split(""""sell_order_summary":"<span class='\"market_commodity_orders_header_promote\"'>""")[1].split(' for sale')[0])) # Sell Quantity
IndexError: list index out of range

汤:

<html><body><p>{"success":1,"sell_order_table":"</p><table class='\"market_commodity_orders_table\"'><tr><th align='\"right\"'>Price</th><th align='\"right\"'>Quantity</th></tr><tr><td align='\"right\"' class='\"\"'>\u00a5 2,225.62</td><td align='\"right\"'>1</td></tr><tr><td align='\"right\"' class='\"\"'>\u00a5 2,249.90</td><td align='\"right\"'>1</td></tr><tr><td align='\"right\"' class='\"\"'>\u00a5 2,258.02</td><td align='\"right\"'>1</td></tr><tr><td align='\"right\"' class='\"\"'>\u00a5 2,274.40</td><td align='\"right\"'>1</td></tr><tr><td align='\"right\"' class='\"\"'>\u00a5 2,299.79</td><td align='\"right\"'>2</td></tr><tr><td align='\"right\"' class='\"\"'>\u00a5 2,300 or more</td><td align='\"right\"'>35","sell_order_summary":"<span class='\"market_commodity_orders_header_promote\"'>41 for sale starting at <span class='\"market_commodity_orders_header_promote\"'>\u00a5 2,225.62","buy_order_table":"<table class='\"market_commodity_orders_table\"'><tr><th align='\"right\"'>Price</th><th align='\"right\"'>Quantity</th></tr><tr><td align='\"right\"' class='\"\"'>\u00a5 2,105.45</td><td align='\"right\"'>1</td></tr><tr><td align='\"right\"' class='\"\"'>\u00a5 1,949.09</td><td align='\"right\"'>1</td></tr><tr><td align='\"right\"' class='\"\"'>\u00a5 1,948.99</td><td align='\"right\"'>2</td></tr><tr><td align='\"right\"' class='\"\"'>\u00a5 1,948.30</td><td align='\"right\"'>1</td></tr><tr><td align='\"right\"' class='\"\"'>\u00a5 1,946.05</td><td align='\"right\"'>1</td></tr><tr><td align='\"right\"' class='\"\"'>\u00a5 1,938 or less</td><td align='\"right\"'>2510","buy_order_summary":"<span class='\"market_commodity_orders_header_promote\"'>2516 requests to buy at <span class='\"market_commodity_orders_header_promote\"'>\u00a5 2,105.45 or lower","highest_buy_order":"210545","lowest_sell_order":"222562","buy_order_graph":[[2105.45,1,"1 buy orders at \u00a5 2,105.45 or higher"],[1949.09,2,"2 buy orders at \u00a5 1,949.09 or higher"],[1948.99,4,"4 buy orders at \u00a5 1,948.99 or higher"],[1948.3,5,"5 buy orders at \u00a5 1,948.30 or higher"],[1946.05,6,"6 buy orders at \u00a5 1,946.05 or higher"],[1938,8,"8 buy orders at \u00a5 1,938 or higher"],[1930,9,"9 buy orders at \u00a5 1,930 or higher"],[1897.65,10,"10 buy orders at \u00a5 1,897.65 or higher"],[1885.66,15,"15 buy orders at \u00a5 1,885.66 or higher"],[1870.29,16,"16 buy orders at \u00a5 1,870.29 or higher"],[1856.45,21,"21 buy orders at \u00a5 1,856.45 or higher"],[1826,22,"22 buy orders at \u00a5 1,826 or higher"],[1806.07,23,"23 buy orders at \u00a5 1,806.07 or higher"],[1800,25,"25 buy orders at \u00a5 1,800 or higher"],[1796.16,26,"26 buy orders at \u00a5 1,796.16 or higher"],[1769.68,27,"27 buy orders at \u00a5 1,769.68 or higher"],[1766,28,"28 buy orders at \u00a5 1,766 or higher"],[1763.37,29,"29 buy orders at \u00a5 1,763.37 or higher"],[1754.06,30,"30 buy orders at \u00a5 1,754.06 or higher"],[1750.46,31,"31 buy orders at \u00a5 1,750.46 or higher"],[1740,32,"32 buy orders at \u00a5 1,740 or higher"],[1734.78,33,"33 buy orders at \u00a5 1,734.78 or higher"],[1730.41,34,"34 buy orders at \u00a5 1,730.41 or higher"],[1728.1,35,"35 buy orders at \u00a5 1,728.10 or higher"],[1721.1,36,"36 buy orders at \u00a5 1,721.10 or higher"],[1719.55,37,"37 buy orders at \u00a5 1,719.55 or higher"],[1715.66,38,"38 buy orders at \u00a5 1,715.66 or higher"],[1712,39,"39 buy orders at \u00a5 1,712 or higher"],[1710.81,44,"44 buy orders at \u00a5 1,710.81 or higher"],[1708,45,"45 buy orders at \u00a5 1,708 or higher"],[1705,46,"46 buy orders at \u00a5 1,705 or higher"],[1701.08,47,"47 buy orders at \u00a5 1,701.08 or higher"],[1700.99,52,"52 buy orders at \u00a5 1,700.99 or higher"],[1700.13,54,"54 buy orders at \u00a5 1,700.13 or higher"],[1696.45,59,"59 buy orders at \u00a5 1,696.45 or higher"],[1693.09,60,"60 buy orders at \u00a5 1,693.09 or higher"],[1691.85,61,"61 buy orders at \u00a5 1,691.85 or higher"],[1671.38,62,"62 buy orders at \u00a5 1,671.38 or higher"],[1649.56,63,"63 buy orders at \u00a5 1,649.56 or higher"],[1635.34,64,"64 buy orders at \u00a5 1,635.34 or higher"],[1610,69,"69 buy orders at \u00a5 1,610 or higher"],[1600,73,"73 buy orders at \u00a5 1,600 or higher"],[1599.99,76,"76 buy orders at \u00a5 1,599.99 or higher"],[1595,78,"78 buy orders at \u00a5 1,595 or higher"],[1588,79,"79 buy orders at \u00a5 1,588 or higher"],[1587,80,"80 buy orders at \u00a5 1,587 or higher"],[1585.63,81,"81 buy orders at \u00a5 1,585.63 or higher"],[1579.99,82,"82 buy orders at \u00a5 1,579.99 or higher"],[1578.51,84,"84 buy orders at \u00a5 1,578.51 or higher"],[1577.41,85,"85 buy orders at \u00a5 1,577.41 or higher"],[1571.48,87,"87 buy orders at \u00a5 1,571.48 or higher"],[1532.16,88,"88 buy orders at \u00a5 1,532.16 or higher"],[1528.78,89,"89 buy orders at \u00a5 1,528.78 or higher"],[1508.12,90,"90 buy orders at \u00a5 1,508.12 or higher"],[1500,92,"92 buy orders at \u00a5 1,500 or higher"],[1499.99,94,"94 buy orders at \u00a5 1,499.99 or higher"],[1493.13,97,"97 buy orders at \u00a5 1,493.13 or higher"],[1489.31,99,"99 buy orders at \u00a5 1,489.31 or higher"],[1470.99,100,"100 buy orders at \u00a5 1,470.99 or higher"],[1457.65,101,"101 buy orders at \u00a5 1,457.65 or higher"],[1437.56,102,"102 buy orders at \u00a5 1,437.56 or higher"],[1436.3,103,"103 buy orders at \u00a5 1,436.30 or higher"],[1428.5,104,"104 buy orders at \u00a5 1,428.50 or higher"],[1427.97,106,"106 buy orders at \u00a5 1,427.97 or higher"],[1426.19,107,"107 buy orders at \u00a5 1,426.19 or higher"],[1422.09,109,"109 buy orders at \u00a5 1,422.09 or higher"],[1399.98,110,"110 buy orders at \u00a5 1,399.98 or higher"],[1395.35,111,"111 buy orders at \u00a5 1,395.35 or higher"],[1393.22,112,"112 buy orders at \u00a5 1,393.22 or higher"],[1386.59,113,"113 buy orders at \u00a5 1,386.59 or higher"],[1364.26,114,"114 buy orders at \u00a5 1,364.26 or higher"],[1359.34,115,"115 buy orders at \u00a5 1,359.34 or higher"],[1356.86,116,"116 buy orders at \u00a5 1,356.86 or higher"],[1350.94,117,"117 buy orders at \u00a5 1,350.94 or higher"],[1350.21,119,"119 buy orders at \u00a5 1,350.21 or higher"],[1336.8,121,"121 buy orders at \u00a5 1,336.80 or higher"],[1333.2,122,"122 buy orders at \u00a5 1,333.20 or higher"],[1319.39,123,"123 buy orders at \u00a5 1,319.39 or higher"],[1283.47,124,"124 buy orders at \u00a5 1,283.47 or higher"],[1250,126,"126 buy orders at \u00a5 1,250 or higher"],[1244.41,129,"129 buy orders at \u00a5 1,244.41 or higher"],[1229.44,130,"130 buy orders at \u00a5 1,229.44 or higher"],[1224.59,132,"132 buy orders at \u00a5 1,224.59 or higher"],[1221.5,133,"133 buy orders at \u00a5 1,221.50 or higher"],[1208.76,134,"134 buy orders at \u00a5 1,208.76 or higher"],[1149.88,136,"136 buy orders at \u00a5 1,149.88 or higher"],[1146.1,137,"137 buy orders at \u00a5 1,146.10 or higher"],[1141.96,139,"139 buy orders at \u00a5 1,141.96 or higher"],[1140.38,140,"140 buy orders at \u00a5 1,140.38 or higher"],[1118.1,142,"142 buy orders at \u00a5 1,118.10 or higher"],[1109.24,144,"144 buy orders at \u00a5 1,109.24 or higher"],[1084.71,145,"145 buy orders at \u00a5 1,084.71 or higher"],[1080.86,146,"146 buy orders at \u00a5 1,080.86 or higher"],[1080.38,147,"147 buy orders at \u00a5 1,080.38 or higher"],[1059.39,149,"149 buy orders at \u00a5 1,059.39 or higher"],[1046.1,150,"150 buy orders at \u00a5 1,046.10 or higher"],[1043,151,"151 buy orders at \u00a5 1,043 or higher"],[995.5,153,"153 buy orders at \u00a5 995.50 or higher"],[976.2,154,"154 buy orders at \u00a5 976.20 or higher"],[955.16,156,"156 buy orders at \u00a5 955.16 or higher"],[924.37,158,"158 buy orders at \u00a5 924.37 or higher"]],"sell_order_graph":[[2225.62,1,"1 sell orders at \u00a5 2,225.62 or lower"],[2249.9,2,"2 sell orders at \u00a5 2,249.90 or lower"],[2258.02,3,"3 sell orders at \u00a5 2,258.02 or lower"],[2274.4,4,"4 sell orders at \u00a5 2,274.40 or lower"],[2299.79,6,"6 sell orders at \u00a5 2,299.79 or lower"],[2300,7,"7 sell orders at \u00a5 2,300 or lower"],[2366.15,8,"8 sell orders at \u00a5 2,366.15 or lower"],[2415.3,9,"9 sell orders at \u00a5 2,415.30 or lower"],[2453.09,10,"10 sell orders at \u00a5 2,453.09 or lower"],[2500.95,11,"11 sell orders at \u00a5 2,500.95 or lower"],[2542.34,12,"12 sell orders at \u00a5 2,542.34 or lower"],[2616.55,13,"13 sell orders at \u00a5 2,616.55 or lower"],[2634.71,14,"14 sell orders at \u00a5 2,634.71 or lower"],[2644.65,15,"15 sell orders at \u00a5 2,644.65 or lower"],[2668.65,16,"16 sell orders at \u00a5 2,668.65 or lower"],[2737.03,17,"17 sell orders at \u00a5 2,737.03 or lower"],[2788.12,18,"18 sell orders at \u00a5 2,788.12 or lower"],[2829.77,19,"19 sell orders at \u00a5 2,829.77 or lower"],[2986.32,20,"20 sell orders at \u00a5 2,986.32 or lower"],[3189.03,21,"21 sell orders at \u00a5 3,189.03 or lower"],[3217.31,22,"22 sell orders at \u00a5 3,217.31 or lower"],[3270.69,23,"23 sell orders at \u00a5 3,270.69 or lower"],[3649.33,24,"24 sell orders at \u00a5 3,649.33 or lower"],[3832.89,25,"25 sell orders at \u00a5 3,832.89 or lower"],[3860.38,26,"26 sell orders at \u00a5 3,860.38 or lower"],[4025,27,"27 sell orders at \u00a5 4,025 or lower"],[4088.37,28,"28 sell orders at \u00a5 4,088.37 or lower"]],"graph_max_y":200,"graph_min_x":924.37,"graph_max_x":4088.37,"price_prefix":"\u00a5","price_suffix":""}</span></span></td></tr></table></span></span></td></tr></table></body></html>

由于某些原因,当我尝试不使用汤而只设置汤='字符串'来重现错误时,它将返回正确的数字43

python beautifulsoup
2个回答
1
投票

您的问题是反斜线。反斜杠(\)字符用于转义具有特殊含义的字符,例如换行符,反斜杠本身或引号字符。反斜杠出现在原始字符串中,但是在您的字符串中却没有,反斜杠转义。这应该可以。

steam.append(int(soup.split("""sell_order_summary":"<span class='\\"market_commodity_orders_header_promote\\"'>""")[1].split(' for sale')[0])) # Sell Quantity

现在,反斜杠包含在拆分字符串中,因为它会被另一个反斜杠转义。

而且这种获取信息的方法实际上并不是最好的方法。我会做这样的事情。

import json

json_obj = json.loads(source.text)
soup = BS(json_obj['sell_order_summary'], 'lxml')
steam = []
steam.append(int(soup.find('span').text))

以JSON格式存储数据(因为您已经将其抓取为JSON),然后使用BeautifulSoup来搜索标签,而不仅仅是从所有内容中提取一个大字符串。


0
投票

您可以使用bs4.find()从ID,类等中查找某些标签。然后,您可以尝试将结果解析为文本。我不确定为什么您遇到soup.split()的问题,但是如果您需要使用它,那么我可以对其进行更多研究。

© www.soinside.com 2019 - 2024. All rights reserved.