我试图复制一个HTML页面中的所有数据,该页面具有BS4的特定类“chapter\u header\u styling”。你知道吗
当我手动输入URL时,这是可行的,但是当有多本书和不同的章节时,这是很乏味的。因此,我创建了另一个脚本,该脚本将生成该书的所有章节url,并将它们组合成一个文本文件bchap.txt
(图书章节)。你知道吗
从那以后,我修改了文件并添加了各种断点,所以忽略我缺少的注释和未使用的数组/列表。我已经把范围缩小到###Comment##
不起作用的地方。可能不是很对,但我不确定。。。我在一定程度上实现了这一点,但不明白为什么它不能将mydivs
数据粘贴到book.html
文件中。如果有谁有更多的经验,可以给我指出正确的方向将不胜感激。你知道吗
#mkbook.py
# coding: utf-8
from bs4 import BeautifulSoup
import requests
LINK = "https://codes.iccsafe.org/content/FAC2017"
pop = ""
#z = ""
chapters = open("bchap.txt",'r')
a = []
for aline in chapters:
chap = aline
#print (chap)
#pop = ""
pop = LINK+chap
#print (pop)
r = requests.get(pop)
data = r.text
#print(data)
soup = BeautifulSoup(data, 'html.parser')
mydivs = soup.findAll("div", {"class": ["annotator", "chapter_header_styling"]})
f = open("BOOK.html","a")
f.write("test <br/>")
########################################
#MY PROBLEM IS BELOW NOT PRINTING DIV DATA INTO TXT FILE
########################################
for div in mydivs:
print (div)
z = str(div)
print(z) #doesn't printout...why???
f.write(z)
print len(mydivs)
f.close()
chapters.close()
##############################################
## this is the old mkbook.py code before I looped it - inputing url 1 @ time
#
# coding: utf-8
from bs4 import BeautifulSoup
import requests
r = requests.get("https://codes.iccsafe.org/content/FAC2017/preface")
data = r.text
soup = BeautifulSoup(data, 'html.parser')
a = []
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()
print len(mydivs) #outputs 1 if copied div data.
#######################################
#mkchap.py
# coding: utf-8
from bs4 import BeautifulSoup
import requests
r = requests.get("https://codes.iccsafe.org/content/FAC2017")
data = r.text
soup = BeautifulSoup(data, 'html.parser')
a = []
soup.findAll('option',{"value":True})
list = soup.findAll('option')
with open('bchap.txt', 'w') as filehandle:
for l in list:
filehandle.write(l['value'])
filehandle.write("\n")
print l['value']
#with open('bchap.txt', 'w') as filehandle:
# filehandle.write("%s\n" % list)
filehandle.close()
问题似乎是您使用了错误的基本url来构建url。你知道吗
如果你看看你的第一个要求,你可以清楚地看到这一点。你知道吗
输出:
运行填充
bchap.txt
的代码后,其输出为让我们先更改基本url,然后重试。你知道吗
输出:
为什么?b'coz的
\n
。如果我们做一个它将输出
你还得把那
\n
去掉。最后的代码是相关问题 更多 >
编程相关推荐