Запись только первой строки в RTF Python 2.7 - PullRequest
0 голосов
/ 19 февраля 2019
import requests
from bs4
import BeautifulSoup as bs

test = 'newtest.rtf'#
out_file = open(test, 'w')
birds = []
with open('BirdsList.txt') as fp:
  for line in fp:
  birds.append(line)

print(birds[183])
def getAnimal(bird):
  r = requests.get('https://www.allaboutbirds.org/guide/' + bird + '/lifehistory')
soup = bs(r.text, 'html.parser')
attributes = soup.find('div', class_ = 'category-content')
print
print
print bird
k = 0
with open('newtest.rtf', 'a') as file:
  for a in attributes:
  if (a.h2['id'] == 'backyardTips'
    or a.h2['id'] == 'credits'):
    break
print
print k
temp = (((a.h2['id']).replace('_', ' ')).capitalize() + "\\b0 :")
test = ""
"{\\rtf\\b "
"" + temp + str(k) + ""
" \\b0\line\Hello my boy\
        }"
""
file.write(test)
k = k + 1
for b in a.find_all('p'):
  print b.text

i = 0
while (i < 3):
  getAnimal(birds[i])
i = i + 1

Я хочу видеть несколько строк в моем RTF, но в настоящее время я вижу только одну.Кто-нибудь знает проблему здесь?Я знаю, что это не проблема с открытием и закрытием файла в неправильном месте.

...