Screen scraper to get all 'a title' elements

Chris Angelico rosuav at gmail.com
Wed Nov 25 17:10:43 EST 2015


On Thu, Nov 26, 2015 at 9:04 AM, ryguy7272 <ryanshuell at gmail.com> wrote:
> Ok, I guess that makes sense.  So, I just tried the script below, and got nothing...
>
> import requests
> from bs4 import BeautifulSoup
>
> r = requests.get("https://en.wikipedia.org/wiki/Wikipedia:Unusual_place_names")
> soup = BeautifulSoup(r.content)
> print soup.find_all("a",{"title"})

The second argument to find_all is supposed to be a dict, not a set,
and it's only useful if you want to put some restriction on the
titles. To simply enumerate all the titles, try this:

[a.get("title") for a in soup.find_all("a")]

ChrisA



More information about the Python-list mailing list