Mehran
Mehran

Reputation: 27

findAll method in BeautifulSoup does not work in Python

in this code, i have a problem. When i print the links, it returns empty list. so code does not correct. what is the problem and how can fix it?

from bs4 import BeautifulSoup
import requests

search = input('Serach for:')
params = {"q": search}
r = requests.get('http://www.bing.com/search', params=params)

soup = BeautifulSoup(r.text, 'html.parser')
results = soup.find("ol", {"id": "b_results"})
links = results.findAll("li", {"class": "b_algo"})

for item in links:
    item_text = item.find("a").text
    item_href = item.find("a").attrs["href"]

    if item_href and item_href:
        print(item_text)
        print(item_href)

Upvotes: 0

Views: 98

Answers (2)

r-beginners
r-beginners

Reputation: 35265

If you change the URL to search to the following content, I think the search results will return.

r = requests.get('https://www.bing.com/?setmkt=en-us&setlang=en-us&', params=params)

Upvotes: 1

Sean Payne
Sean Payne

Reputation: 1961

When I run your code, it gives me a prompt "search for:"

If I type 'dog' and then hit enter, I get a list back

dogの意味・使い方・読み方 | Weblio英和辞書
https://ejje.weblio.jp/content/dog
dogの意味 - 英和辞典 - コトバンク
https://kotobank.jp/ejword/dog
dogの意味 - goo辞書 英和和英
https://dictionary.goo.ne.jp/word/en/dog/
dog - ウィクショナリー日本語版 - Wiktionary
https://ja.wiktionary.org/wiki/dog
【楽天市場】犬服のiDog アウトレット(プチプラドッ …
https://item.rakuten.co.jp/idog/c/0000000129/
with DOG|仙台のトリミングサロン・ペットホテル・ …
https://withdog-sendai.jp/
D・O・G WEBSTORE-犬服SHOP-
http://d-o-gweb.com/
DOG inTheパラレルワールドオーケストラ OFFICIAL SITE
http://inu-para.com/
【楽天市場】ドッグフードカタログ|愛犬にあったフー …
https://event.rakuten.co.jp/pet/food/dog/
ドック - Wikipedia
https://ja.wikipedia.org/wiki/%E3%83%89%E3%83%83%E3%82%AF

This would indicate that your program is working. Are you typing in something to search for when you run this via command line?

Upvotes: 1

Related Questions