【问题标题】:Unable to extract and attribute value from webpage with python无法使用python从网页中提取和属性值
【发布时间】:2021-04-15 08:11:02
【问题描述】:

我正在尝试从中提取属性,但我必须做错事,因为我无法得到它。

这是我的代码

import requests
from bs4 import BeautifulSoup

url = 'https://www.pccomponentes.com/procesadores'

headers = ({'User-Agent':
            'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36',
            'Accept-Language': 'es-ES, es;q=0.5'})

page = requests.get(url,headers=headers)
soup = BeautifulSoup(page.content,'html.parser')
product = soup.find('div',class_ = 'col-xs-6 col-sm-4 col-md-4 col-lg-4')
#print(product)

brand = product.find('a', attrs={'class': 'c-product-card__title-link cy-product-link'})['data-brand']
print(brand)

目标是获得“AMD”。

任何帮助将不胜感激。

问候

【问题讨论】:

    标签: python web-scraping beautifulsoup


    【解决方案1】:

    以下暗示 Cloudflare 已将您对网站的请求检测为自动机器人,随后拒绝您访问该应用程序。

    因此,您不会找到任何预期的信息,但您可以查看Python's requests triggers Cloudflare's security while urllib does not

    【讨论】:

    • 感谢 Hedgehog 的 cmets。如果我运行以下代码:`brand = product.header.h3.a print(brand)`它返回整个文本,包括我想要的数据,当我抓取价格、产品名称等时,代码运行良好。跨度>
    猜你喜欢
    • 2021-10-22
    • 1970-01-01
    • 2021-03-12
    • 1970-01-01
    • 1970-01-01
    • 2021-03-18
    • 2022-01-25
    • 2020-10-09
    • 1970-01-01
    相关资源
    最近更新 更多