1

Beautiful Soupとurllibを使用して、Pythonスクリプトからyahoo検索エンジンで基本的なクエリを実行したいと思います。私はグーグルについても同じことをしましたが、それはかなり簡単でしたが、ヤフーは少し難しいことを証明しています。yahoo検索エンジンへのクエリの最小限のサンプルスクリプトが役立ちます。ありがとうございました!

4

2 に答える 2

2

まず、避けてくださいurllib-代わりにリクエストを使用してください。それははるかに健全なインターフェースです。

次に、返されたページのすべてのリンクにはyschttl、スキームlink-1に続くクラスと IDlink-2が含まれます。美しいスープで使用できるもの:

import requests
from bs4 import BeautifulSoup
url = "http://search.yahoo.com/search?p=%s"
query = "python"
r = requests.get(url % query) 
soup = BeautifulSoup(r.text)
soup.find_all(attrs={"class": "yschttl"})

for link in soup.find_all(attrs={"class": "yschttl"}):
    print "%s (%s)" %(link.text, link.get('href'))

私たちに与えます

Python Programming Language – Official Website (http://www.python.org/)
Python - Image Results (http://images.search.yahoo.com/search/images?_adv_prop=image&va=python)
Python (programming language) - Wikipedia, the free encyclopedia (http://en.wikipedia.org/wiki/Python_(programming_language))

もっと。

于 2012-05-12T09:53:56.130 に答える