Python Beautifulsoup, get href tag, in a tag
·
Answer a question
i had problem with getting href tag, so my case like this, this is the html file :
<div class="list-product with-sidebar">
<a class="frame-item" href="./produk-a.html" target="_blank" title="Produk A">
</a>
<a class="frame-item" href="./produk-b.html" target="_blank" title="Produk B">
</a>
</div>
so here my code
def get_category_item_list(category):
base_url = 'https://www.website.com/'
res = session.get(base_url+category)
res = BeautifulSoup(res.content, 'html.parser')
all_title = res.findAll('a', attrs={'class':'frame-item'})
data_titles = []
for title in all_title:
product_link = title.get('a')['href']
data_titles.append(product_link)
return data_titles
what i want to get is, href links.. like this
produk-a.html
produk-b.html
when i try to run it.. it wont let me get link on href, they give error code :
TypeError: 'NoneType' object is not subscriptable
Answers
I believe that your problem lies in this line :
product_link = title.get('a')['href']
You already have a list of "a" elements, so you probably just need :
product_link = title['href']
更多推荐

所有评论(0)