简体   繁体   English

我无法使用 Beautifulsoup 获得 a 标签,但我可以获得其他标签

[英]I can't get the a tag using Beautifulsoup, though I can get other tags

I'm just trying to get data from a webpage called "Elgiganten" and its url: https://www.elgiganten.se/我只是想从一个名为“Elgiganten”的网页及其 url: https://www.elgiganten.se/获取数据

I want to get the products name and its url.我想获取产品名称及其 url。 When I tried to get the a tag so I got an empty list, but I could get the span tag though taht they were in the same div tag.当我尝试获取 a 标签时,我得到了一个空列表,但我可以获取 span 标签,尽管它们在同一个 div 标签中。

Here is the whole code:这是整个代码:

from bs4 import BeautifulSoup
import requests


respons = requests.get("https://www.elgiganten.se")

soup = BeautifulSoup(respons.content, "lxml")

g_data = soup.find_all("div", {"class": "col-flex S-order-1"})

for item in g_data:
    print(item.contents[1].find_all("span")[0])
    print(item.contents[1].find_all("a", {"class": "product-name"}))

I hope that anyone can tell me why the a tag seems to be invisible, and can fix the issue.我希望任何人都可以告诉我为什么 a 标签似乎是不可见的,并且可以解决这个问题。

Go for the a-tags directly. Go 直接用于 a-tags。 You can extract the product name and the url both from that tag:您可以从该标签中提取产品名称和 url:

from bs4 import BeautifulSoup
import requests

respons = requests.get("https://www.elgiganten.se")
soup = BeautifulSoup(respons.content, "lxml")

g_data = soup.find_all("a", {"class": "product-name"}, href=True)

for item in g_data:
  print(item['title'], item['href'])

If you wish to stick to the way you started, the following is how you can achieve that:如果你想坚持你开始的方式,以下是你如何实现它:

import requests
from bs4 import BeautifulSoup

respons = requests.get("https://www.elgiganten.se")
soup = BeautifulSoup(respons.text,"lxml")

for item in soup.find_all(class_="mini-product-content"):
    product_name = item.find("span",class_="table-cell").text
    product_link = item.find("a",class_="product-name").get("href")
    print(product_name,product_link)

Try:尝试:

g_data = soup.find_all("a", class_="product-name")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM