Home > other >  Consult: Python indexer error: list indices of scope
Consult: Python indexer error: list indices of scope

Time:05-17

The import requests
The import pandas as pd
The from bs4 import BeautifulSoup
Num=0


Def get_text (url) :
Try:
Headers={
"The user-agent:" 'the Mozilla/5.0 (Windows NT 10.0; Win64. X64) AppleWebKit/537.36 (KHTML, like Gecko)
''the Chrome/85.0.4183.102 Safari/537.36', 'Accept - Language:' useful - CN '
}
R=requests. Get (url, headers=headers)
R.r aise_for_status ()
R.e ncoding=of state Richard armitage pparent_encoding
Return r.t ext
Except:
Return "crawl site failure!"


Def run (game_info, jump_link game_evaluation, text) :
Soup=BeautifulSoup (text, ". The HTML parser ")

# game evaluation
W=soup. Find_all (class_="col search_reviewscore responsive_secondrow")
For u in w:
If u.s pan is not None:
Game_evaluation. Append (
U.s pan [r]. "data - tooltip - HTML" split (" & lt; Br>" ) ", "[0] + + u.s pan [r]." data - tooltip - HTML "split (" & lt; Br>" ) [1])
The else:
Game_evaluation. Append (" no evaluation!" )

# game details page link
Link_text=soup. Find_all (" div "id=" search_resultsRows ")
For k in link_text:
B=k.f ind_all (' a ')
For j in b:
Jump_link. Append (j [' href '])


# name and priceGlobal num
Name_text=soup. Find_all (' div 'class_="responsive_search_name_combined")
For z in name_text:
The price of # each game
Name=z.f ind (class_="title"). The string. The strip ()
# to determine whether a discount for the extraction of None, prices
If z.f ind (class_="col search_discount responsive_secondrow"). The string is None:
Price=z.f ind (class_="col search_price discounted responsive_secondrow"). The text. The strip (). The split (" RMB ")
Game_info. Append ([num + 1, name, price, [2]. The strip (), game_evaluation [num], jump_link [num]])
The else:
Price=z.f ind (class_="col search_price responsive_secondrow"). The string. The strip (). The split (" RMB ")
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=[' list ', 'game', 'the present price RMB game', 'game evaluation', 'game page links'])
Df. To_csv (save_path, index=0)
Print (" file success!" )


If __name__=="__main__" :
All of the information Game_info=[] # game
Turn_link=[] # page link
Jump_link=[] # game details page link
Game_evaluation=[] # game rate and evaluation
For I in range (1, 11) :
Turn_link. Append (" https://store.steampowered.com/search/? Tags=3859% c1685%2 c3843 & amp; Category1=998 & amp; Category3=2 c9%1% 2 c38 & amp; OS=win& The filter=topsellers& Page=1 "+ STR (" & amp; Page="+ STR (I)))
The run (Game_info Jump_link, Game_evaluation get_text (Turn_link] [I - 1))
Save_data (Game_info)


The following mistakes
Traceback (the most recent call last) :
The File "C: \ Users \ DD \ Desktop \ 4489489. Py", line 70, in & lt; module>
The run (Game_info Jump_link, Game_evaluation get_text (Turn_link] [I - 1))
The File "C: \ Users \ DD \ Desktop \ 4489489. Py", line 52, in the run
Game_info. Append ([num + 1, name, price, [1], game_evaluation [num], jump_link [num]])
IndexError: list index out of range
  • Related