求教:Python索引器错误:列表索引超出范围

w990181939 2021-05-16 02:17:25
import requests
import pandas as pd
from bs4 import BeautifulSoup
num = 0


def get_text(url):
try:
headers = {
"User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/85.0.4183.102 Safari/537.36', 'Accept-Language': 'zh-CN '
}
r = requests.get(url, headers=headers)
r.raise_for_status()
r.encoding = r.apparent_encoding
return r.text
except:
return "爬取网站失败!"


def run(game_info, jump_link, game_evaluation, text):
soup = BeautifulSoup(text, "html.parser")

# 游戏评价
w = soup.find_all(class_="col search_reviewscore responsive_secondrow")
for u in w:
if u.span is not None:
game_evaluation.append(
u.span["data-tooltip-html"].split("<br>")[0] + "," + u.span["data-tooltip-html"].split("<br>")[-1])
else:
game_evaluation.append("暂无评价!")

# 游戏详情页面链接
link_text = soup.find_all("div", id="search_resultsRows")
for k in link_text:
b = k.find_all('a')
for j in b:
jump_link.append(j['href'])

# 名字和价格
global num
name_text = soup.find_all('div', class_="responsive_search_name_combined")
for z in name_text:
# 每个游戏的价格
name = z.find(class_="title").string.strip()
# 判断折扣是否为None,提取价格
if z.find(class_="col search_discount responsive_secondrow").string is None:
price = z.find(class_="col search_price discounted responsive_secondrow").text.strip().split("¥")
game_info.append([num + 1, name, price[2].strip(), game_evaluation[num], jump_link[num]])
else:
price = z.find(class_="col search_price responsive_secondrow").string.strip().split("¥")
game_info.append([num + 1, name, price[1], game_evaluation[num], jump_link[num]])
num = num + 1


def save_data(game_info):
save_path = "D:/Steam.csv"
df = pd.DataFrame(game_info, columns=['排行榜', '游戏名字', '目前游戏价格¥', '游戏评价', '游戏页面链接'])
df.to_csv(save_path, index=0)
print("文件保存成功!")


if __name__ == "__main__":
Game_info = [] # 游戏全部信息
Turn_link = [] # 翻页链接
Jump_link = [] # 游戏详情页面链接
Game_evaluation = [] # 游戏好评率和评价
for i in range(1, 11):
Turn_link.append("https://store.steampowered.com/search/?tags=3859%2C1685%2C3843&category1=998&category3=1%2C9%2C38&os=win&filter=topsellers&page=1" + str("&page=" + str(i)))
run(Game_info, Jump_link, Game_evaluation, get_text(Turn_link[i-1]))
save_data(Game_info)


出现以下错误
Traceback (most recent call last):
File "C:\Users\DD\Desktop\4489489.py", line 70, in <module>
run(Game_info, Jump_link, Game_evaluation, get_text(Turn_link[i-1]))
File "C:\Users\DD\Desktop\4489489.py", line 52, in run
game_info.append([num + 1, name, price[1], game_evaluation[num], jump_link[num]])
IndexError: list index out of range
...全文
62 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

37,721

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧