メインページにリンク、著者、投稿日を含むすべての記事をキャプチャするには、次のようにします。これを辞書に保存するか、簡単な操作のためにパンダのデータフレームに保存することができます。
from bs4 import BeautifulSoup
import requests
baseurl = 'http://www.fiercepharma.com'
response = requests.get(baseurl)
soup = BeautifulSoup(response.content)
cdict = {}
for group in soup.find_all('div', {'class' : 'card horizontal views-row'}):
try:
title = group.find('h2', {'class' : 'field-content list-title'}).text
link = baseurl + group.find('h2', {'class' : 'field-content list-title'}).find('a', href=True)['href']
author = group.find('span', {'class' : 'field-content'}).find('a').text
time = group.find('span', {'class' : 'field-content'}).find('time').text
content = group.find('p', {'class' : 'field-content card-text'}).text
cdict[link] = {'title' : title, 'author' : author, 'time' : time, 'content' : content}
except AttributeError as e:
print('[-] Unable to parse {}'.format(e))
print(cdict)
#{'http://www.fiercepharma.com/manufacturing/lonza-bulks-up-5-5b-deal-for-capsugel': {'author': u'Eric Palmer',
# 'content': u'Swiss CDMO Lonza has pulled the trigger on a $5.5 billion deal to acquire the U.S.-based contract capsule and drug producer Capsugel to create another sizable\u2026',
# 'time': u'Dec 15, 2016 8:45am',
# 'title': u'Lonza bulks up with $5.5B deal for Capsugel'},
thats cos souping div.region.region-contentはデータ全体を1つの要素として与えます。コードを試してみましょう。 – rrmerugu
あなたの 'div_sub'部分を' div_sub = main_div.select( "。card.horizontal.views-row") 'で置き換えてください。うまくいきます。 – rrmerugu
ありがとう - 「div.card.horizontal.views-row」の先頭の「div」をなぜ掛けているのか教えていただけますか? ...私はそのタグをフロントのdivと一緒に使ってみました。 –