【问题标题】:How to capture data from website as key-value pairs from the website using python?如何使用python从网站捕获数据作为网站的键值对?
【发布时间】:2021-12-25 14:41:12
【问题描述】:

生成的输出:1 用于获取模型名称的代码:2

enter code here
test_link = 'https://www.amd.com/en/products/cpu/amd-ryzen-9-3900xt'
r = requests.get(test_link, headers=headers)
soup = BeautifulSoup(r.content,'lxml')
whole_data = soup.find('div', class_='fieldset-wrapper')
specifications = []
specifications_value=[]
for variable1 in whole_data.find_all('div', class_='field__label'):
    #print(variable1.text)
    variable1 = variable1.text
    specifications = list(variable1.split('\n'))
    #print(specifications)
for variable2 in whole_data.find_all('div', class_='field__item'):
    #print(variable2.text)
    variable2 = variable2.text
    specifications_value = list(variable2.split('\n'))
    #print(specifications_value)

问题:我正在获取数据,但在单独的变量和 for 循环中,如何使用键值对映射这两个变量?这样我就可以检查以下条件: 如果该值是平台,则仅说明它的值(盒处理器)

我想以这样一种方式捕获数据,如果“键”是平台,那么只捕获它的值(盒装处理器)。其他 14 个标签也是如此。

【问题讨论】:

  • 请不要在屏幕截图中包含文字。 Stack Overflow 有许多 formatting features,您可以使用它们在问题中包含代码、输出等文本。考虑用包含实际文本的代码块替换您的文本屏幕截图。
  • 这能回答你的问题吗? How to iterate through two lists in parallel? 您想同时迭代 whole_data.find_all('div', class_='field__label')whole_data.find_all('div', class_='field__item')
  • @PranavHosangadi 谢谢!是的,我想遍历这 2 个。但我还想检查如果第一个列表是 == 平台,那么只从第二个列表中选择值。否则将其留空。例如:如果产品系列不存在,那么我必须将其留空。两者都在不同的列表中,我将如何映射这些?
  • @PranavHosangadi 你能帮忙吗
  • 当您并行迭代这两个而不是像我共享的链接中所示的单独循环时,您将获得specifications 的一个值和specifications_value 的相应值。

标签: python dictionary beautifulsoup key-value


【解决方案1】:

您可以遍历预期键列表并使用:-soup-contains 定位描述节点。如果那不是无,则选择子值。否则,返回 ''。

import requests
from bs4 import BeautifulSoup as bs

links = ['https://www.amd.com/en/products/cpu/amd-ryzen-7-3800xt',
         'https://www.amd.com/en/products/cpu/amd-ryzen-9-3900xt']

all_keys = ['Platform', 'Product Family', 'Product Line', '# of CPU Cores',
            '# of Threads', 'Max. Boost Clock', 'Base Clock', 'Total L2 Cache', 'Total L3 Cache',
            'Default TDP', 'Processor Technology for CPU Cores', 'Unlocked for Overclocking', 'CPU Socket',
            'Thermal Solution (PIB)', 'Max. Operating Temperature (Tjmax)', 'Launch Date', '*OS Support']

with requests.Session() as s:

    s.headers = {'User-Agent': 'Mozilla/5.0'}

    for link in links:

        r = s.get(link)
        soup = bs(r.content, 'lxml')
        specification = {}

        for key in all_keys:

            spec = soup.select_one(
                f'.field__label:-soup-contains("{key}") + .field__item, .field__label:-soup-contains("{key}") + .field__items .field__item')

            if spec is None:
                specification[key] = ''
            else:
                if key == '*OS Support':
                    specification[key] = [
                        i.text for i in spec.parent.select('.field__item')]
                else:
                    specification[key] = spec.text

        print(specification)
        print()

【讨论】:

  • 感谢您提供此代码。因为我是新手,所以我无法理解它。我把它分成小部分并试图理解。
  • 我寻找具有所需搜索文本的类 .field__item 的元素,例如'平台'。如果存在,我将移动到相邻元素并获取其值或子值(如果操作系统规范信息)。如果那个特定的规格描述,例如CPU Socket 不在给定的网页上,那么 spec 的值为 None ,所以我返回 ''
  • 感谢所有帮助!我明白了。我希望有一天我也能像你一样写代码。
  • 我还有 1 个查询当我尝试使用它在以下输出中给出的代码时,如何将 Model 也包含为键:{'Model': [AMD Ryzen™ 7 3800XT |台式机处理器 | AMD]}
  • 看起来您需要将 .text 添加到规范的末尾,即分配给字典时的规范文本。
猜你喜欢
  • 1970-01-01
  • 2020-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-05
  • 1970-01-01
  • 2012-07-22
相关资源
最近更新 更多