0
import bs4
import requests
import re

r = requests.get('https://www.the961.com/latest-news/lebanon-news/').text

soup = bs4.BeautifulSoup(r, 'lxml')

for article in soup.find_all('article'):

    title = article.h3.text
    print(title)

    date = soup.find('span', class_='byline-part date').text
    print(date)

// 它只返回它到达的第一个链接并坚持下去。我怎样才能让它返回下一个链接。

    link = soup.find('h3', class_='title').a['href']
    print(link)

    print(link)

    author = soup.find('span', class_="byline-part author").text
    print(author)
4

1 回答 1

1

也许尝试article.find代替soup.find.

import bs4
import requests
import lxml

r = requests.get('https://www.the961.com/latest-news/lebanon-news/').text

soup = bs4.BeautifulSoup(r, 'lxml')

for article in soup.find_all('article'):

    title = article.h3.text
    print(title)

    date = article.find('span', class_='byline-part date')
    if date: print('date', date.text)

    link = article.find('h3', class_='title').a['href']
    print(link)

    author = article.find('span', class_="byline-part author")
    if author: print('author', author.text)

    print()
于 2021-04-28T15:39:21.843 回答