2016-08-18 5 views
1

私は、eBay特集コレクションに入るPythonとBeautifulSoupを使用してWebスクレイピングツールを構築しようとしていますが、コレクション内のすべての製品のURLを取得しますいくつかは多かれ少なかれ)。ここで私は私のコードでこすりしようとコレクションのURLです:http://www.ebay.com/cln/ebayhomeeditor/Surface-Study/324079803018商品ページのリンクのためのeBay特集コレクションの掻き立て

は、ここに私のコードは、これまでのところです:

import requests 
from bs4 import BeautifulSoup 

url = 'http://www.ebay.com/cln/ebayhomeeditor/Surface-Study/324079803018' 
soup = BeautifulSoup(requests.get(url).text, 'html.parser') 

product_links = [] 

item_thumb = soup.find_all('div', attrs={'class':'itemThumb'}) 
for link in item_thumb: 
    product_links.append(link.find('a').get('href')) 

print product_links 

このスクレーパーは、リストproduct_linksへの17のリンクを追加する必要があります。ただし、途中でしか動作しません。具体的には、毎回最初の12個の製品リンクのみを削り取り、17個のリンクがすべて同じHTMLタグと属性内にあるにもかかわらず、残りの5個をそのまま残します。の関数である何

<script escape-xml="true"> 
     if (typeof(collectionState) != 'object') { 
      var collectionState = { 
       itemImageSize: {sWidth: 280, sHeight: 280, lWidth: 580, lHeight: 620}, 
       page: 1, 
       totalPages: 2, 
       totalItems: 17, 
       pageId: '2057253', 
       currentUser: '', 
       collectionId: '323101965012', 
       serviceHost: 'svcs.ebay.com/buying/collections/v1', 
       owner: 'ebaytecheditor', 
       csrfToken: '', 
       localeId: 'en-US', 
       siteId: 'EBAY-US', 
       countryId: 'US', 
       collectionCosEnabled: 'true', 
       collectionCosHostExternal: 'https://api.ebay.com/social/collection/v1', 
       collectionCosEditEnabled: 'true', 
       isCollectionReorderEnabled: 'false', 
       isOwnerSignedIn: false || false, 
       partiallySignedInUser: '@@[email protected]@[email protected]@', 
       baseDomain: 'ebay.com', 
       currentDomain: 'www.ebay.com', 
       isTablet: false, 
       isMobile: false, 
       showViewCount: true 
      }; 
     } 
    </script> 

:ページのHTMLコードをより詳しく見ると、私が見つけた唯一の違いは、最初の12個のリンクと最後の5は、私がここに含まれているXMLスクリプトの部分で分離されているということですこのスクリプト?このスクリプトが私のスクレイパーが最終的な5つのリンクを擦るのを怠った理由は可能でしょうか?これを回避して最後の5つを描く方法はありますか?

+0

JavaScriptを使用して次の5つのリンクが読み込まれるため、これが起こっています。 –

答えて

0

最後の数がhttp://www.ebay.com/cln/_ajax/2/ebayhomeeditor/324079803018AJAXリクエストによって生成されます。

enter image description here

URLがebayhomeeditorとどのようないくつかの製品ID でなければなりませんを使用して作られていますあなたが訪問したページの元のURLの両方。

データを取得するために不可欠なパラメータは、itemsPerPageですが、残りの部分を再生して、その効果を確認することができます。だから、すべてのURLを取得するためにそれを一緒に入れて

['http://www.ebay.com/itm/yamazaki-home-tower-book-end-white-stationary-holder-desktop-organizing-steel/171836462366?hash=item280240551e', 'http://www.ebay.com/itm/tetris-constructible-interlocking-desk-lamp-neon-light-nightlight-by-paladone/221571335719?hash=item3396ae4627', 'http://www.ebay.com/itm/iphone-docking-station-dock-native-union-new-in-box/222202878086?hash=item33bc52d886', 'http://www.ebay.com/itm/turnkey-pencil-sharpener-silver-office-home-school-desk-gift-peleg-design/201461359979?hash=item2ee808656b', 'http://www.ebay.com/itm/himori-weekly-times-desk-notepad-desktop-weekly-scheduler-30-weeks-planner/271985620013?hash=item3f539b342d'] 

を::

In [23]: params = {"itemsPerPage": "10"} 

In [24]: with requests.Session() as s: 
    ....:   soup1 = BeautifulSoup(s.get('http://www.ebay.com/cln/ebayhomeeditor/Surface-Study/324079803018').content, 
    ....:        "html.parser") 
    ....:   main_urls = [a["href"] for a in soup1.select("div.itemThumb div.itemImg.image.lazy-image a[href]")] 
    ....:   soup2 = BeautifulSoup(s.get("http://www.ebay.com/cln/_ajax/2/ebayhomeeditor/324079803018", params=params).content, 
    ....:        "html.parser") 
    ....:   print(len(main_urls)) 
    ....:   main_urls.extend(a["href"] for a in soup2.select("div.itemThumb div.itemImg.image.lazy-image a[href]")) 
    ....:   print(main_urls) 
    ....:   print(len(main_urls)) 
    ....:  
12 
['http://www.ebay.com/itm/archi-desk-accessories-pen-cup-designed-by-hsunli-huang-for-moma/262435041373?hash=item3d1a58f05d', 'http://www.ebay.com/itm/moorea-seal-violet-light-crane-scissors/201600302323?hash=item2ef0507cf3', 'http://www.ebay.com/itm/kikkerland-photo-holder-with-6-magnetic-wooden-clothespin-mh69-cable-47-long/361394782932?hash=item5424cec2d4', 'http://www.ebay.com/itm/authentic-22-design-studio-merge-concrete-pen-holder-desk-office-pencil/331846509549?hash=item4d4397e3ed', 'http://www.ebay.com/itm/supergal-bookend-by-artori-design-ad103-metal-black/272273290322?hash=item3f64c0b452', 'http://www.ebay.com/itm/elago-p2-stand-for-ipad-tablet-pcchampagne-gold/191527567203?hash=item2c97eebf63', 'http://www.ebay.com/itm/this-is-ground-mouse-pad-pro-ruler-100-authentic-natural-retail-100/201628986934?hash=item2ef2062e36', 'http://www.ebay.com/itm/hot-fuut-foot-rest-hammock-under-desk-office-footrest-mini-stand-hanging-swing/152166878943?hash=item236dda4edf', 'http://www.ebay.com/itm/unido-silver-white-black-led-desk-office-lamp-adjustable-neck-brightness-level/351654910666?hash=item51e0441aca', 'http://www.ebay.com/itm/in-house-black-desk-office-organizer-paper-clips-memo-notes-monkey-business/201645856763?hash=item2ef30797fb', 'http://www.ebay.com/itm/rifle-paper-co-2017-maps-desk-calendar-illustrated-worldwide-cities/262547131670?hash=item3d21074d16', 'http://www.ebay.com/itm/muji-erasable-pen-black/262272348079?hash=item3d10a66faf', 'http://www.ebay.com/itm/rifle-paper-co-2017-maps-desk-calendar-illustrated-worldwide-cities/262547131670?hash=item3d21074d16', 'http://www.ebay.com/itm/muji-erasable-pen-black/262272348079?hash=item3d10a66faf', 'http://www.ebay.com/itm/yamazaki-home-tower-book-end-white-stationary-holder-desktop-organizing-steel/171836462366?hash=item280240551e', 'http://www.ebay.com/itm/tetris-constructible-interlocking-desk-lamp-neon-light-nightlight-by-paladone/221571335719?hash=item3396ae4627', 'http://www.ebay.com/itm/iphone-docking-station-dock-native-union-new-in-box/222202878086?hash=item33bc52d886', 'http://www.ebay.com/itm/turnkey-pencil-sharpener-silver-office-home-school-desk-gift-peleg-design/201461359979?hash=item2ee808656b', 'http://www.ebay.com/itm/himori-weekly-times-desk-notepad-desktop-weekly-scheduler-30-weeks-planner/271985620013?hash=item3f539b342d'] 
19 

In [25]: 

だけにセットを使用するので、返されるものとほとんど重複があり、あなたを与えるだろう

params = {"itemsPerPage": "10"} 
soup= BeautifulSoup(requests.get("http://www.ebay.com/cln/_ajax/2/ebayhomeeditor/324079803018", params=params).content) 
print([a["href"] for a in soup.select("div.itemThumb div.itemImg.image.lazy-image a[href]")]) 

リストにmain_urlsまたはコールセットを保存します。

なぜそれが起こっているのか分かりませんし、実際にそれを理解しようとしていない場合は、最初の呼び出しの後にmain_urlsの長さを引いて、ajax呼び出しから返された「totalItems:17」を解析してから0,私はそれについてあまり心配しないだろう。

+0

これを行うためのEBay APIはありますか? – pratibha

関連する問題