あなたはrequests
でそれを解決した場合、あなたは「負荷もっと」ボタンをクリックするとんどのようなブラウザを模倣する必要がある - それはウェブを維持し、あなたのコードでそれをシミュレートし、http://www.goudengids.be/q/ajax/business/results.json
エンドポイントにXHRリクエストを送信スクレイピングセッション。 XHRレスポンスはJSON形式でされている - すべてでこの場合はBeautifulSoup
のための必要はありません: - 終了条件を把握 -
import requests
main_url = "http://www.goudengids.be/qn/business/advanced/where/Provincie%20Antwerpen/what/restaurant/"
xhr_url = "http://www.goudengids.be/q/ajax/business/results.json"
with requests.Session() as session:
session.headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36'}
# visit main URL
session.get(main_url)
# load more listings - follow the pagination
page = 1
listings = []
while True:
params = {
"input": "restaurant Provincie Antwerpen",
"what": "restaurant",
"where": "Provincie Antwerpen",
"type": "DOUBLE",
"resultlisttype": "A_AND_B",
"page": str(page),
"offset": "2",
"excludelistingids": "nl_BE_YP_FREE_11336647_0000_1746702_6165_20130000, nl_BE_YP_PAID_11336647_0000_1746702_7575_20139729427, nl_BE_YP_PAID_720348_0000_187688_7575_20139392980",
"context": "SRP * A_LIST"
}
response = requests.get(xhr_url, params=params, headers={
"X-Requested-With": "XMLHttpRequest",
"Referer": main_url
})
data = response.json()
# collect listing names in a list (for example purposes)
listings.extend([item["bn"] for item in data["overallResult"]["searchResults"]])
page += 1
# TODO: figure out exit condition for the while True loop
print(listings)
私はあなたのための重要なTODOを残してきたリストの収集を停止したとき。
私はあなたのスクリプトを実行したとき、それは(最後の最新の呼び出し)私は、エラーメッセージトレースバックを与えたに: ファイル「C:\ Users \ユーザーユーザー\デスクトップ\ Pythonの\スクリプト\の3url.py」 とでは、3行目、セッションとしてのセッション(): NameError:name 'requests'が定義されていませんどのように修正できますか? –
vishnu
@vishnuこの 'import requests'行は一番上にありますか?これは重要。そして 'requests'モジュールをインストールしなければなりません。 – alecxe
あなたは正しい@alecxe私は本当に忘れてしまった。あなたの大きな助けをありがとう、また私は将来あなたを必要とします – vishnu