[Python] I want links and all the content from each link

Discussão em 'Python' iniciado por Stack, Março 17, 2025.

  1. Stack

    Stack Membro Participativo

    I searched for a keyword (cybersecurity) on a newspaper website and the results show around 10 articles. I want my code to grab the link and go to that link and get the whole article and repeat this to all the 10 articles in the page. (I don't want the summary, I want the whole article)

    import urllib.request
    import ssl
    import time
    from bs4 import BeautifulSoup

    ssl._create_default_https_context = ssl._create_unverified_context
    pages = [1]
    for page in pages:
    data = urllib.request.urlopen("https://www.japantimes.co.jp/tag/cybersecurity/page/{}".format(page))
    soup = BeautifulSoup(data, 'html.parser')

    for article in soup.find_all('div', class_="content_col"):
    link = article.p.find('a')
    print(link.attrs['href'])

    for link in links:
    headline = link.h1.find('div', class_= "padding_block")
    headline = headline.text
    print(headline)
    content = link.p.find_all('div', class_= "entry")
    content = content.text
    print(content)

    print()

    time.sleep(3)


    This is not working.

    date = link.li.find('time', class_= "post_time")


    Showing error :


    AttributeError: 'NoneType' object has no attribute 'find'
    ​

    This code is working and grabbing all the articles links. I want to include code that will add headline and content from every article link.

    import urllib.request
    import ssl
    import time
    from bs4 import BeautifulSoup

    ssl._create_default_https_context = ssl._create_unverified_context
    pages = [1]
    for page in pages:

    data = urllib.request.urlopen("https://www.japantimes.co.jp/tag/cybersecurity/page/{}".format(page))

    soup = BeautifulSoup(data, 'html.parser')

    for article in soup.find_all('div', class_="content_col"):
    link = article.p.find('a')
    print(link.attrs['href'])
    print()
    time.sleep(3)

    Continue reading...

Compartilhe esta Página