1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Scraping a URL using Beautiful Soup

Discussão em 'Python' iniciado por Stack, Novembro 23, 2025.

  1. Stack

    Stack Membro Participativo

    I am beginner in data scraping.

    In this case, I want to get a URL like "https:// . . .", but the result is a list in a link variable that contains of all links on the web. Here is the code below;

    import requests
    from bs4 import BeautifulSoup
    url = 'https://www.detik.com/search/searchall?query=KPK'
    page = requests.get(url)
    soup = BeautifulSoup(page.content, 'html.parser')
    artikel = soup.findAll('div', {'class' : 'list media_rows list-berita'})
    p = 1
    link = []
    for p in artikel:
    s = p.findAll('a', href=True)['href']
    link.append(s)


    The result of the code above is getting an error such as:

    TypeError Traceback (most recent call last)
    <ipython-input-141-469cb6eabf70> in <module>
    3 link = []
    4 for p in artikel:
    5 s = p.findAll('a', href=True)['href']
    6 link.append(s)
    TypeError: list indices must be integers or slices, not str


    The result is I want to get all links of https:// . . . in <div class = 'list media_rows list-berita' as a list

    Continue reading...

Compartilhe esta Página