2017-11-22 7 views
3

Python 3を使って「もっと見るキャンペーン」からリンクを集めるには?私はこのページからすべての260604リンクを集めたいと思っていますか?これは、さらに以下のようにページを要求するために使用することができウェブページからすべてのリンクを集めるには?

https://www.gofundme.com/mvc.php?route=category/loadMoreTiles&page=2&term=sport&country=GB&initialTerm= 

https://www.gofundme.com/mvc.php?route=category&term=sport

+1

*常に質問に目を向けるだけの場合は、一般的な[python]タグを使用してください。 –

答えて

1

retrieve links from web page using python and BeautifulSoup

import httplib2 
from bs4 import BeautifulSoup, SoupStrainer 

http = httplib2.Http() 
status, response = http.request('https://www.gofundme.com/mvc.php?route=category&term=sport') 

for link in BeautifulSoup(response, parseOnlyThese=SoupStrainer('a')): 
    if link.has_attr('href'): 
     print (link['href']) 
+0

OPが望むすべての資金調達キャンペーンのリンクは集められず、ページに最初に掲載されたキャンペーンのみが集められます。 – hoefling

2

View More Campaignsボタンをクリックすることから、ブラウザには、以下のURLを要求します:

あなたのように始まる出力を与える
from bs4 import BeautifulSoup  
import requests 

page = 1 
links = set() 
length = 0 

while True: 
    print("Page {}".format(page)) 
    gofundme = requests.get('https://www.gofundme.com/mvc.php?route=category/loadMoreTiles&page={}&term=sport&country=GB&initialTerm='.format(page)) 
    soup = BeautifulSoup(gofundme.content, "html.parser") 
    links.update([a['href'] for a in soup.find_all('a', href=True)]) 

    # Stop when no new links are found 
    if len(links) == length: 
     break 

    length = len(links) 
    page += 1 

for link in sorted(links): 
    print(link) 

https://www.gofundme.com/100-round-kumite-rundraiser 
https://www.gofundme.com/10k-challenge-for-disabled-sports 
https://www.gofundme.com/1yeti0 
https://www.gofundme.com/2-marathons-1-month 
https://www.gofundme.com/23yq67t4 
https://www.gofundme.com/2fwyuwvg 

は返されたリンクの一部が重複しているので、setは、これを回避するために使用されます。 新しいリンクが表示されなくなるまで、スクリプトは新しいページを要求し続けます。これは約18ページで発生します。

関連する問題