【问题标题】:deleting some parts of requested href attrs in bs4删除 bs4 中请求的 href attrs 的某些部分
【发布时间】:2021-04-24 03:44:05
【问题描述】:

我需要一个页面上所有文章的 slug。我使用 bs4 来获取所有文章的 href 内容,但是有些文章的链接有另一个我不需要的 URL。我想删除这些项目。我使用了这段代码:

import requests
import re
from bs4 import BeautifulSoup



r = requests.get('https://davidventuri.medium.com/')


soup = BeautifulSoup(r.text, 'html.parser')
all_slugs = soup.find_all('a', {'class': 'dn br'})

for i in range(len(all_slugs)):
    slug = all_slugs[i]['href']
    print(slug)

这是我获得hrefs的结果:

/this-is-not-a-real-data-science-degree-d170c660c1cf

/not-a-real-degree-data-science-curriculum-2021-19ba9af2c1d4

/bitcoin-learning-path-9ed73f2f11d9

/your-first-day-of-school-eaf363b19ded

https://medium.com/free-code-camp/an-overview-of-every-data-visualization-course-on-the-internet-9ccf24ea9c9b

https://medium.com/free-code-camp/the-best-data-science-courses-on-the-internet-ranked-by-your-reviews-6dc5b910ea40

https://medium.com/free-code-camp/every-single-machine-learning-course-on-the-internet-ranked-by-your-reviews-3c4a7b8026c0

https://medium.com/free-code-camp/dive-into-deep-learning-with-these-23-online-courses-bf247d289cc0

/how-ai-is-revolutionizing-mental-health-care-a7cec436a1ce

https://medium.com/free-code-camp/i-ranked-all-the-best-data-science-intro-courses-based-on-thousands-of-data-points-db5dc7e3eb8e

其实我想要的如下:

/this-is-not-a-real-data-science-degree-d170c660c1cf

/not-a-real-degree-data-science-curriculum-2021-19ba9af2c1d4

/bitcoin-learning-path-9ed73f2f11d9

/your-first-day-of-school-eaf363b19ded

/an-overview-of-every-data-visualization-course-on-the-internet-9ccf24ea9c9b

/the-best-data-science-courses-on-the-internet-ranked-by-your-reviews-6dc5b910ea40

/every-single-machine-learning-course-on-the-internet-ranked-by-your-reviews-3c4a7b8026c0

/dive-into-deep-learning-with-these-23-online-courses-bf247d289cc0

/how-ai-is-revolutionizing-mental-health-care-a7cec436a1ce

/i-ranked-all-the-best-data-science-intro-courses-based-on-thousands-of-data-points-db5dc7e3eb8e

如何使用正则表达式或其他方法删除它们?

【问题讨论】:

    标签: python regex beautifulsoup python-requests


    【解决方案1】:

    如果replace 的子字符串始终相同,则可以不使用regex,如下所示:

    slug = a['href'].replace('https://medium.com/free-code-camp','')
    

    示例

    import requests
    from bs4 import BeautifulSoup
    
    r = requests.get('https://davidventuri.medium.com/')
    soup = BeautifulSoup(r.text, 'html.parser')
    
    all_slugs = soup.find_all('a', {'class': 'dn br'})
    
    for a in all_slugs:
        slug = a['href'].replace('https://medium.com/free-code-camp','')
        print(slug)
    

    输出

    /this-is-not-a-real-data-science-degree-d170c660c1cf
    /not-a-real-degree-data-science-curriculum-2021-19ba9af2c1d4
    /bitcoin-learning-path-9ed73f2f11d9
    /your-first-day-of-school-eaf363b19ded
    /an-overview-of-every-data-visualization-course-on-the-internet-9ccf24ea9c9b
    /the-best-data-science-courses-on-the-internet-ranked-by-your-reviews-6dc5b910ea40
    /every-single-machine-learning-course-on-the-internet-ranked-by-your-reviews-3c4a7b8026c0
    /dive-into-deep-learning-with-these-23-online-courses-bf247d289cc0
    /how-ai-is-revolutionizing-mental-health-care-a7cec436a1ce
    /i-ranked-all-the-best-data-science-intro-courses-based-on-thousands-of-data-points-db5dc7e3eb8e
    

    编辑

    你也可以使用split()

    slug = a['href'].split('/')[-1]
    

    示例

    import requests
    from bs4 import BeautifulSoup
    
    r = requests.get('https://davidventuri.medium.com/')
    soup = BeautifulSoup(r.text, 'html.parser')
    
    all_slugs = soup.find_all('a', {'class': 'dn br'})
    
    for a in all_slugs:
        slug = a['href'].split('/')[-1]
        print(slug)
    

    【讨论】:

    • 实际上问题是它们并不总是恒定的。
    • @Hani:编辑了我的答案并添加了split() 作为非正则表达式解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-22
    • 1970-01-01
    • 2019-01-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多