Раздел сайта не отображается в BeautifulSoup - PullRequest
0 голосов
/ 14 ноября 2018

Я пытаюсь создать абстрактную часть этого сайта:

from bs4 import BeautifulSoup
urlLink = 'https://www.cfapubs.org/doi/abs/10.2469/faj.v74.n4.2'
page_response = requests.get(page_link, timeout=5, verify=False, headers={'User-Agent': 'Mozilla/5.0'})
soup2 = BeautifulSoup(page_response.content, 'html.parser')

и когда я ищу:

    soup2.find_all("div", {"class": "abstractSection"})

Я ничего не понимаю, тогда как эта часть меня интересует. Есть идеи?

1 Ответ

0 голосов
/ 14 ноября 2018

Я не уверен, где вы нашли это page_link для использования. Попробуйте следующий подход, чтобы получить контент, который вы хотите проанализировать.

from bs4 import BeautifulSoup
import requests

urlLink = 'https://www.cfapubs.org/doi/abs/10.2469/faj.v74.n4.2'

page_response = requests.get(urlLink,headers={'User-Agent':'Mozilla/5.0'})
soup = BeautifulSoup(page_response.content, 'html.parser')
name = soup.find(class_="hlFld-ContribAuthor").find("a").text
abstract = soup.find(class_="abstractSection").find("p").text
print(f'Name : {name}\nAbstract : {abstract}')

Если вы хотите использовать селектор, попробуйте:

from bs4 import BeautifulSoup
import requests

urlLink = 'https://www.cfapubs.org/doi/abs/10.2469/faj.v74.n4.2'

page_response = requests.get(urlLink,headers={'User-Agent':'Mozilla/5.0'})
soup = BeautifulSoup(page_response.content, 'html.parser')
name = soup.select_one(".hlFld-ContribAuthor a").text
abstract = soup.select_one(".abstractSection p").text
print(f'Name : {name}\nAbstract : {abstract}')

Выход:

Name : Charles D. Ellis, CFA
Abstract :  One of the consequences of the shift in corporate retirement plans from defined benefit           to defined contribution is widespread retirement insecurity. Although most people in the           top one-third of economic affluence will be fine, for the other two-thirds—particularly           the bottom one-third—the problem is a serious threat. We can prevent this painful           future if we act sensibly and soon by raising the alarm with our corporate and government           leaders.

Наконец, если вы не хотите видеть разрыв между текстом внутри abstract, замените строку на abstract = ' '.join(soup.find(class_="abstractSection").find("p").text.split()).

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...