【问题标题】:How to extract these links with BeautifulSoup?如何用 BeautifulSoup 提取这些链接?
【发布时间】:2020-07-27 14:55:50
【问题描述】:

我正在尝试从<a class="hwd_sound sound audio_play_button icon-volume-up ptr" title="Pronunciation for " data-src-mp3="https://www.collinsdictionary.com/sounds/hwd_sounds/EN-GB-W0037420.mp3" data-lang="en_GB"></a> 中提取下载链接。我成功了re.findall('data-src-mp3="(.*?)"', str(content1))

我想请求一个带有BeautifulSoup 的方法,这使得我的代码需要更少的库。以下是我的完整代码:

import requests
from bs4 import BeautifulSoup
import re

url = 'https://www.collinsdictionary.com/dictionary/english-french/graduate'
headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0'}
soup = BeautifulSoup(requests.get(url, headers = headers).content, 'html.parser')

for tag in soup.select('''
        script,
        .hcdcrt,
        #ad_contentslot_1,
        #ad_contentslot_2,
        div.h2_entry,
        div.copyright,
        div.example-info,
        div.share-overlay,
        div.popup-overlay,
        span.xr'''):
    tag.extract()  
   
content1 = ''.join(map(str, soup.select_one('.cB.cB-def.dictionary.biling').contents))

sound_url = re.findall('data-src-mp3="(.*?)"', str(content1))
print(sound_url)

和我的方法的结果

['https://www.collinsdictionary.com/sounds/hwd_sounds/EN-GB-W0037420.mp3', 'https://www.collinsdictionary.com/sounds/hwd_sounds/FR-W0037420.mp3', 'https://www.collinsdictionary.com/sounds/hwd_sounds/FR-W0071410.mp3', 'https://www.collinsdictionary.com/sounds/hwd_sounds/fr_bachelier.mp3', 'https://www.collinsdictionary.com/sounds/hwd_sounds/63854.mp3']

非常感谢!

【问题讨论】:

  • 您是否只是想获得相同的输出但不使用re 还是别的什么?
  • @JackFleeting 我正在尝试使用BeautifulSoup 获得相同的输出。
  • 该页面中有大约 30 个 mp3 链接 - 您想要所有这些链接还是有特定的子集?
  • 检查我的答案@LAD
  • @JackFleeting 好问题,我限制在content1

标签: python-3.x beautifulsoup web-crawler


【解决方案1】:

试试这个:

import requests
from bs4 import BeautifulSoup

url = 'https://www.collinsdictionary.com/dictionary/english-french/graduate'
headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0'}
soup = BeautifulSoup(requests.get(url, headers=headers).content, 'html.parser')

for tag in soup.select('''
        script,
        .hcdcrt,
        #ad_contentslot_1,
        #ad_contentslot_2,
        div.h2_entry,
        div.copyright,
        div.example-info,
        div.share-overlay,
        div.popup-overlay,
        span.xr'''):
    tag.extract()

content1 = ''.join(map(str, soup.select_one('.cB.cB-def.dictionary.biling').contents))

sound_url = [link.get('data-src-mp3') for link in soup.select('a[data-src-mp3$=".mp3"]')]
print(sound_url)

打印:

['https://www.collinsdictionary.com/sounds/hwd_sounds/EN-GB-W0037420.mp3', 'https://www.collinsdictionary.com/sounds/hwd_sounds/FR-W0037420.mp3', 'https://www.collinsdictionary.com/sounds/hwd_sounds/FR-W0071410.mp3', 'https://www.collinsdictionary.com/sounds/hwd_sounds/fr_bachelier.mp3', 'https://www.collinsdictionary.com/sounds/hwd_sounds/63854.mp3', 'https://www.collinsdictionary.com/sounds/hwd_sounds/EN-GB-W0037420.mp3', 'https://www.collinsdictionary.com/sounds/hwd_sounds/EN-US-W0037420.mp3', 'https://www.collinsdictionary.com/sounds/hwd_sounds/AR-W0037420.mp3', 'https://www.collinsdictionary.com/sounds/hwd_sounds/PT-BR-W0037420.mp3', 'https://www.collinsdictionary.com/sounds/hwd_sounds/ZH-CN-W0037420.mp3', 'https://www.collinsdictionary.com/sounds/hwd_sounds/HR-W0037420.mp3', 'https://www.collinsdictionary.com/sounds/hwd_sounds/CS-W0037420.mp3', 'https://www.collinsdictionary.com/sounds/hwd_sounds/DA-W0037420.mp3', 'https://www.collinsdictionary.com/sounds/hwd_sounds/NL-W0037420.mp3', 'https://www.collinsdictionary.com/sounds/hwd_sounds/ES-ES-W0037420.mp3', 'https://www.collinsdictionary.com/sounds/hwd_sounds/FI-W0037420.mp3', 'https://www.collinsdictionary.com/sounds/hwd_sounds/FR-W0037420.mp3', 'https://www.collinsdictionary.com/sounds/hwd_sounds/DE-W0037420.mp3', 'https://www.collinsdictionary.com/sounds/hwd_sounds/EL-W0037420.mp3', 'https://www.collinsdictionary.com/sounds/hwd_sounds/IT-W0037420.mp3', 'https://www.collinsdictionary.com/sounds/hwd_sounds/JA-W0037420.mp3', 'https://www.collinsdictionary.com/sounds/hwd_sounds/KO-W0037420.mp3', 'https://www.collinsdictionary.com/sounds/hwd_sounds/NO-W0037420.mp3', 'https://www.collinsdictionary.com/sounds/hwd_sounds/PL-W0037420.mp3', 'https://www.collinsdictionary.com/sounds/hwd_sounds/PT-PT-W0037420.mp3', 'https://www.collinsdictionary.com/sounds/hwd_sounds/RU-W0037420.mp3', 'https://www.collinsdictionary.com/sounds/hwd_sounds/ES-419-W0037420.mp3', 'https://www.collinsdictionary.com/sounds/hwd_sounds/SV-W0037420.mp3', 'https://www.collinsdictionary.com/sounds/hwd_sounds/TH-W0037420.mp3', 'https://www.collinsdictionary.com/sounds/hwd_sounds/TR-W0037420.mp3', 'https://www.collinsdictionary.com/sounds/hwd_sounds/VI-W0037420.mp3']

【讨论】:

  • 我将你的代码sound_url = [link.get('data-src-mp3') for link in soup.select('a[data-src-mp3$=".mp3"]')]修改为sound_url = [link.get('data-src-mp3') for link in content1 .select('a[data-src-mp3$=".mp3"]')],但是出现了错误。有简单的解决方法吗?
  • 我的意思是soup -> content1
  • content1 不是 BeautifulSoup 对象,不能使用 css 选择器处理。您可以再次使用 BeautifulSoup soup1 = BeautifulSoup(content1, 'html.parser')sound_url = [link.get('data-src-mp3') for link in soup1.select('a[data-src-mp3$=".mp3"]')] 来做汤。
  • 非常感谢!我明白了。
猜你喜欢
  • 2016-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-19
相关资源
最近更新 更多