sho*_*han 5 html python beautifulsoup web-scraping
我在获取href标签时遇到问题,所以我的情况是这样的,这是文件html:
<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>
Run Code Online (Sandbox Code Playgroud)
所以这是我的代码
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
Run Code Online (Sandbox Code Playgroud)
我想要得到的是href链接..像这样
produk-a.html
produk-b.html
Run Code Online (Sandbox Code Playgroud)
当我尝试运行它时..它不会让我获得链接href,他们给出错误代码:
TypeError: 'NoneType' object is not subscriptable
Run Code Online (Sandbox Code Playgroud)
我相信你的问题出在这一行:
product_link = title.get('a')['href']
Run Code Online (Sandbox Code Playgroud)
您已经有了“a”元素的列表,因此您可能只需要:
product_link = title['href']
Run Code Online (Sandbox Code Playgroud)