I've been trying to access the title attribute in a tag of a website. But for some reason this piece of code gives me an error:
html_data=requests.get("https://www.olx.com.pk/items/q-white-bantam").content
html_data=BeautifulSoup(html_data, "lxml")
ads=html_data.find_all("li", {"aria-label":"Listing"})[0]
ads=ads.find_all("a")
tag=ads.find("div")["title"]
print(tag)
Any idea what I'm doing wrong?
CodePudding user response:
You can select title
attribute in tag as follows:
import requests
from bs4 import BeautifulSoup
html_data=requests.get("https://www.olx.com.pk/items/q-white-bantam")
soup=BeautifulSoup(html_data.content, "lxml")
ads=soup.find_all("li", {"aria-label":"Listing"})[1:]
for ad in ads:
title=ad.select_one('._41d2b9f3 a').get('title')
print(title)
Output:
Silk Bantam Cochin Blue columbian Brahma white Aseel Blue motled bantm
Black Bantam White Bantam bentum
White Bantam chicks for sale
3 days old Light Sussex F1 Breed chicks available or white Bantam
white Bantam eggs
03127826350 White bantam dreeders set for sale 1500 pr pice
A quality white bantam healthy and active chicks
Eggs available. (golden/blue/white buff, Plymouth, Sussex, bantam)
white and blue bantam chicks
White Bantam, Golden Bantam, black Bantam
Cochin/cochine Bantams/Bantums Chicks available, black blue white
White Bantam Chicks
white bantam murga available for sale
white bantam
Bantam For Sale urgent White And Black Only Male
white bantam pair
bantam Breader white bantam and black bantam
Black Tail White Japanese Bantam Pair
White Bantam, golden Bantam, black Bantam, white silikey, White polish
CodePudding user response:
There are different issues in your code:
- You should iterate your
ResultSet
to usefind()
on every element to avoid:
AttributeError: ResultSet object has no attribute 'find'. You're probably treating a list of elements like a single element. Did you call find_all() when you meant to call find()?
There is no
<div>
with attributtitle
it is in the<a>
:<a href="/item/black-and-white-bantam-for-sale-iid-1048730374" title="Black and White bantam for Sale"><div ></div></a>
Example
import requests
from bs4 import BeautifulSoup
html_data=requests.get("https://www.olx.com.pk/items/q-white-bantam").content
html_data=BeautifulSoup(html_data, "lxml")
for ad in html_data.find_all("li", {"aria-label":"Listing"}):
print(ad.a.get('title'))
### or longform
### print(ad.find('a').get('title'))
Output
Silk Bantam Cochin Blue columbian Brahma white Aseel Blue motled bantm
Black and White bantam for Sale
Black Bantam White Bantam bentum
White Bantam chicks for sale
...