2017-04-21 8 views
0

私はこのゲームのウェブサイト(g2a [ドット] com)を掻き集めて、私が探しているゲームのベスト価格のリストを取得しようとしています。価格は通常テーブルの中にあります(画像参照)。BeautifulSoupでg2a [dot] comを掻き集める

for gTitle in gameList: 
    page = urllib.request.urlopen('http://www.g2a.com/%s.html' %gTitle).read() 
    soup = BeautifulSoup(page, 'lxml') 
    table = soup.find('table',class_='mp-user-rating') 

しかし、私は表を印刷するとき、私はPythonが一緒に内容のいずれかなしにウェブサイト内のすべてのテーブルを合併していることを見つける:テーブルに取得する

g2a printscreen

私のコードは次のとおりです。

>>> <table class="mp-user-rating jq-wh-offers wh-table"></table> 

これはバグですか、何か間違っていますか?私はBeautifulSoup4とurllibでPython 3.6.1を使用しています。私は可能な限りこれらを使い続けたいと思っていますが、私は変えたいです。

+3

必要なものはjavascriptで生成され、bsを使用して取得することはできません。セレンの使用を検討するhttps://selenium-python.readthedocs.io/ –

答えて

0

私はSeleniumを試してみましたが、実際にはその作業が完了しました。ありがとう、ペドロ!興味のある人には、私のコード:

# importing packages 
from selenium import webdriver 

# game list 
gameList = ['mass-effect-andromeda-origin-cd-key-preorder-global',\ 
      'total-war-warhammer-steam-cd-key-preorder-global',\ 
      'starcraft-2-heart-of-the-swarm-cd-key-global-1'] 

# scraping 
chromePath = r"C:\Users\userName\Documents\Python\chromedriver.exe" 
for gTitle in gameList: 
    driver = webdriver.Chrome(chromePath) 
    driver.get('http://www.g2a.com/%s.html' %gTitle) 
    table = driver.find_element_by_xpath("""//*[@id="about-game"]/div/div[3]/div[1]/table/tbody""") 
    bestPrice = ''.join(list(table.text.split('\n'))[2][12:][:6]) 
    bestPrice = float(bestPrice.replace(",",".")) 
    print(bestPrice) 
0

私はそのウェブサイトを調べました。 「LOAD MORE」以降をクリックすると、ゲームのリストがロードされます。 inspect要素の中にブラウザのネットワークタブを調べ、 "xhr"リクエストのみをフィルタリングすると、新しいゲームセットを読み込むためにapiエンドポイントが表示されます。私は私のURLとしてこのapiエンドポイントを使用しました。

import requests,json 
pageNum = 0 # start with 0, (Also using lower than 0 will start it from 0) 
while True : 
    url = "https://www.g2a.com/lucene/search/filter?&minPrice=0.00&maxPrice=10000&cn=&kr=&stock=all&event=&platform=0&search=&genre=0&cat=0&sortOrder=popularity+desc&start={}&rows=12&steam_app_id=&steam_category=&steam_prod_type=&includeOutOfStock=&includeFreeGames=false&_=1492758607443".format(str(pageNum)) 

    games_list = json.loads(requests.get(url).text)['docs'] # `games_list` contains each game as a dictionary from where you can take out the required information. 

    if len(games_list) == 0: 
     break # we break off here when the maximum of start parameter is reached and the games_list is empty. 
    else: 
     pageNum += 12 # we use an increment of 12 because we observed an increment of 12 in the start parameter each time we click on "LOAD MORE" 
関連する問題