【问题标题】:BeautifulSoup//Python3.9.6// *iterate over soup and save extracted data to dict*BeautifulSoup//Python3.9.6// *遍历汤并将提取的数据保存到dict*
【发布时间】:2021-10-13 12:06:36
【问题描述】:

出于好奇,我开始使用 selenium 和 BeautifulSoup。下面的代码对我来说很好用。我只想知道是否有更好的//更短的方法将数据保存到字典,而不是使用两个单独的列表和for循环。

该代码通过 ISBN-13 号码在亚马逊上搜索一本书,然后获取有关该书的一些信息。

代码的这一部分

def get_item_data(user_input):
   item_url = get_item_url(user_input)
   driver.get(item_url)
   soup = BeautifulSoup(driver.page_source, 'html.parser')

   # scrape html page for needed item data
   results_key  = soup.find_all('div',{'class':'a-section a-spacing-small a-text-center rpi-attribute-label'})
   results_value = soup.find_all('div',{'class':'a-section a-spacing-none a-text-center rpi-attribute-value'})

   #loop over results and extract specific data
   #save extracted data to list
   key_list = []
   value_list = []
   for data in results_key:
      key = data.span.text
      key_list.append(key)

   for data in results_value:
      value = data.span.text
      value_list.append(value)

   # combine lists into dict
   data_dict = dict(zip(key_list,value_list)

输出

[WDM] - ====== WebDriver manager ======
[WDM] - Current google-chrome version is 94.0.4606
[WDM] - Get LATEST driver version for 94.0.4606   
[WDM] - Driver [C:\Users\Ignaz\.wdm\drivers\chromedriver\win32\94.0.4606.61\chromedriver.exe] found in cache

DevTools listening on ws://127.0.0.1:50547/devtools/browser/598bf196-9909-4a5d-a47e-94e5d28f96f4
[17784:18480:1013/134257.078:ERROR:display_layout.cc(559)] PlacementList must be sorted by first 8 bits of display_id 



<<------------------------------------------>>
[<div class="a-section a-spacing-small a-text-center rpi-attribute-label">       
<span>Seitenzahl der Print-Ausgabe</span>
</div>, <div class="a-section a-spacing-small a-text-center rpi-attribute-label">
<span>Sprache</span>
</div>, <div class="a-section a-spacing-small a-text-center rpi-attribute-label">
<span>Herausgeber</span>
</div>, <div class="a-section a-spacing-small a-text-center rpi-attribute-label">
<span>Erscheinungstermin</span>
</div>, <div class="a-section a-spacing-small a-text-center rpi-attribute-label">
<span>Abmessungen</span>
</div>, <div class="a-section a-spacing-small a-text-center rpi-attribute-label">
<span>ISBN-10</span>
</div>, <div class="a-section a-spacing-small a-text-center rpi-attribute-label">
<span>ISBN-13</span>
<<------------------------------------------>>
</div>] [<div class="a-section a-spacing-none a-text-center rpi-attribute-value">
<span>256 Seiten</span>
</div>, <div class="a-section a-spacing-none a-text-center rpi-attribute-value">
<span>Englisch</span>
</div>, <div class="a-section a-spacing-none a-text-center rpi-attribute-value">
<span>Profile Books</span>
</div>, <div class="a-section a-spacing-none a-text-center rpi-attribute-value">
<span>3. August 2017</span>
</div>, <div class="a-section a-spacing-none a-text-center rpi-attribute-value">
<span>12.8 x 2 x 19.4 cm</span>
</div>, <div class="a-section a-spacing-none a-text-center rpi-attribute-value">
<span>1781257027</span>
</div>, <div class="a-section a-spacing-none a-text-center rpi-attribute-value">
<span>978-1781257029</span>
</div>]


#results_key list
<<------------------------------->>
['Seitenzahl der Print-Ausgabe', 'Sprache', 'Herausgeber', 'Erscheinungstermin', 'Abmessungen', 'ISBN-10', 'ISBN-13']
<<------------------------------->>
#results_value list
['256 Seiten', 'Englisch', 'Profile Books', '3. August 2017', '12.8 x 2 x 19.4 cm', '1781257027', '978-1781257029']
<<------------------------------->>
# zipped-dict
{'Seitenzahl der Print-Ausgabe': '256 Seiten', 'Sprache': 'Englisch', 'Herausgeber': 'Profile Books', 'Erscheinungstermin': '3. August 2017', 'Abmessungen': '12.8 x 2 x 19.4 cm', 'ISBN-10': '1781257027', 'ISBN-13': '978-1781257029'}
<<------------------------------->>

完整代码

from bs4 import BeautifulSoup
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(ChromeDriverManager().install())

# function to compute right url out of user input
def get_item_url(search_term):
   template = 'https://www.amazon.de/s?k={}&__mk_de_DE=%C3%85M%C3%85%C5%BD%C3%95%C3%91&ref=nb_sb_noss'
   search_term = search_term.replace("-","")
   url = template.format(search_term)
   # open driver and get item href
   driver.get(url)
   soup = BeautifulSoup(driver.page_source,'html.parser')
   # scrape page for specific href of item
   results = soup.find_all('h2',{'class','a-size-mini a-spacing-none a-color-base s-line-clamp-2'})
   item = results[0]
   item_href = item.a.get('href')
   item_url = 'https://www.amazon.de' + item_href
   return item_url


# function to get item data 
def get_item_data(user_input):
   item_url = get_item_url(user_input)
   driver.get(item_url)
   soup = BeautifulSoup(driver.page_source, 'html.parser')
   # scrape html page for needed item data
   results_key  = soup.find_all('div',{'class':'a-section a-spacing-small a-text-center rpi-attribute-label'})
   results_value = soup.find_all('div',{'class':'a-section a-spacing-none a-text-center rpi-attribute-value'})
   print('<<------------------------------------------>>')
   print(results_key, results_value)
   print('<<------------------------------------------>>')
   #loop over results and extract specific data
   #save extracted data to list
   key_list = []
   value_list = []
   for data in results_key:
      key = data.span.text
      key_list.append(key)

   for data in results_value:
      value = data.span.text
      value_list.append(value)
   # combine lists into dict
   data_dict = dict(zip(key_list,value_list))
   driver.close()

   print(key_list)
   print('<<------------------------------->>')
   print(value_list)
   print('<<------------------------------->>')
   print(data_dict)
   print('<<------------------------------->>')
   
   return data_dict

data_list = get_item_data('978-3-86882-504-6')

【问题讨论】:

  • 你可以使用字典理解。

标签: python loops dictionary web-scraping beautifulsoup


【解决方案1】:

您可以使用字典理解并在单行代码中获取字典。

data_dict = { key.span.text: val.span.text for (key, val) in zip(results_key, results_value) }

【讨论】:

  • 谢谢,这正是我想知道的。我确信有办法做得更好,但我想不通。祝你有美好的一天 ! :D
猜你喜欢
  • 1970-01-01
  • 2023-02-08
  • 1970-01-01
  • 2022-11-14
  • 1970-01-01
  • 1970-01-01
  • 2016-01-08
  • 2021-02-11
  • 2020-09-16
相关资源
最近更新 更多