【问题标题】:Why is BeautifulSoup4 missing the first file URL?为什么 BeautifulSoup4 缺少第一个文件 URL?
【发布时间】:2019-01-08 20:54:53
【问题描述】:

我正在尝试将本网站上的文件编目作为个人练习。当我运行以下代码时,我不知道为什么我没有在该网站上获得第一个文件 url。任何帮助表示赞赏。

import requests
from bs4 import BeautifulSoup
import regex

url = 'https://www.liberliber.it/online/autori/autori-p/niccolo-paganini/24-capricci-per-violino-solo-op-1/'
resp = requests.get(url)
soup = BeautifulSoup(resp.text, 'lxml')

files = soup.find_all(href=regex.compile("\.mp3$"))

for h in files:
    a = h.findNext('a')
    #print(a.string)
    urls.append(a.attrs['href'])
    tags.append(a.string)

files 最终偏移一个 mp3 文件。为什么我没有得到第一个文件并在最后添加另一个文件?

【问题讨论】:

    标签: python beautifulsoup python-requests


    【解决方案1】:

    我认为您不需要findNext,因为您在files 中拥有所有a 标签。所以也许你只是想要

    for h in files:
        urls.append(h.attrs['href'])
        tags.append(h.string)
    

    【讨论】:

    • 我的荣幸。我自己在这个网站上得到了很多帮助。
    猜你喜欢
    • 2021-08-23
    • 1970-01-01
    • 1970-01-01
    • 2018-06-29
    • 1970-01-01
    • 2012-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多