Кажется, проблема в том, что вы строите свой URL-адрес, используя неверный базовый URL-адрес.
LINK = "https://codes.iccsafe.org/content/FAC2017"
Если вы посмотрите на свой 1-й запрос, вы увидите это ясно.
print(pop)
print(r.status_code)
Выходы:
https://codes.iccsafe.org/content/FAC2017/content/FAC2017
404
После запуска кода для заполнения bchap.txt
его вывод будет
/content/FAC2017
/content/FAC2017/legend
/content/FAC2017/copyright
/content/FAC2017/preface
/content/FAC2017/chapter-1-application-and-administration
/content/FAC2017/chapter-2-scoping-requirements
/content/FAC2017/chapter-3-building-blocks
/content/FAC2017/chapter-4-accessible-routes
/content/FAC2017/chapter-5-general-site-and-building-elements
/content/FAC2017/chapter-6-plumbing-elements-and-facilities
/content/FAC2017/chapter-7-communication-elements-and-features
/content/FAC2017/chapter-8-special-rooms-spaces-and-elements
/content/FAC2017/chapter-9-built-in-elements
/content/FAC2017/chapter-10-recreation-facilities
/content/FAC2017/list-of-figures
/content/FAC2017/fair-housing-accessibility-guidelines-design-guidelines-for-accessible-adaptable-dwellings
/content/FAC2017/advisory
Позволяет сначала изменить базовый URL и повторить попытку.
from bs4 import BeautifulSoup
import requests
LINK = "https://codes.iccsafe.org"
pop = ""
chapters = open("bchap.txt",'r')
a = []
for aline in chapters:
chap = aline
pop = LINK+chap
r = requests.get(pop)
print(pop)
print(r.status_code)
chapters.close()
Выходы:
https://codes.iccsafe.org/content/FAC2017
404
...
почему?b'coz of \n
.Если мы сделаем
print(repr(pop))
Он выдаст
'https://codes.iccsafe.org/content/FAC2017\n'
Вам придется также убрать \n
.Окончательный код, который работал, это
from bs4 import BeautifulSoup
import requests
LINK = "https://codes.iccsafe.org"
pop = ""
chapters = open("bchap.txt",'r')
a = []
for aline in chapters:
chap = aline
pop = LINK+chap
r = requests.get(pop.strip())
data = r.text
soup = BeautifulSoup(data, 'html.parser')
mydivs = soup.findAll("div", class_="annotator chapter_header_styling")
f = open("BOOK.html","a")
for div in mydivs:
z = str(div)
f.write(z)
f.close()
chapters.close()