Python - 将多个 HTML 页面解析为单个 TXT 文件
我正在尝试将 X 个 HTML 文件中的特定内容解析为单个 TXT 文件。
我已经肮脏地编码了以下内容:
#!/usr/bin/python
import sys, mechanize, BeautifulSoup
def parsedata():
##do stuff
prvitekst = soup.find(text='Random Number')
prvikesh = prvitekst.findNextSiblings('td')
drugitekst = soup.find(text='Random Month/Yeare')
drugikesh = drugitekst.findNextSiblings('td')
trechitekst = soup.find(text='Small Random Number')
trechikesh = trechitekst.findNextSiblings('td')
content = prvikesh + ";" + drugikesh + ";" + trechikesh + ";"
writeFile(content);
def readFile(id):
fi = open('result/page-%s.html' % id, 'r');
def writeFile(content):
f = open('parsed.txt', 'a')
f.write(content,"\n")
f.close();
def main(start):
##initialize vars
id = int(start)
page = readFile(id)
soup = BeautifulSoup(page)
print soup.prettify()
readFile(id)
for id in range(1000000000):
parsedata()
id = id + 1
continue
main(sys.argv[1]);
虽然我试图抓取的 HTML 部分看起来像这样,
<tr style="height:40px; background-color:#f0f0f0;"><td colspan="4" class="textLargeBold" style="border-bottom: solid 1px #c4c4c4;">Random Details</td></tr>
<tr class="text">
<td align="left" valign="top"><b>Type</b></td>
<td align="left" valign="top">Color</td>
<td align="left" valign="top"><b>Random Number</b></td>
<td align="left" valign="top">213523123123123</td>
</tr>
<tr class="text"
<td align="left" valign="top"><b>Random Month/Year</b></td>
<td align="left" valign="top">12/13</td>
<td align="left" valign="top"><b>Small Random Number</b></td>
<td align="left" valign="top">13233</td>
</tr>
但我想要第一个之后的详细信息。因此,如果我正在搜索 Typem,我希望它向我显示颜色。
当然,最后我希望以类似于 CSV 的格式解析获取的内容。
类型;随机数;随机月/年
它当然应该
Color;213523123123123;12/13
在我已经编写的代码中解析,我不搜索类型,但这可以很容易地改变。
编辑:固定意图
I'm trying to parse specific content from an X number of HTML files to a single TXT file.
I have dirtily coded the following:
#!/usr/bin/python
import sys, mechanize, BeautifulSoup
def parsedata():
##do stuff
prvitekst = soup.find(text='Random Number')
prvikesh = prvitekst.findNextSiblings('td')
drugitekst = soup.find(text='Random Month/Yeare')
drugikesh = drugitekst.findNextSiblings('td')
trechitekst = soup.find(text='Small Random Number')
trechikesh = trechitekst.findNextSiblings('td')
content = prvikesh + ";" + drugikesh + ";" + trechikesh + ";"
writeFile(content);
def readFile(id):
fi = open('result/page-%s.html' % id, 'r');
def writeFile(content):
f = open('parsed.txt', 'a')
f.write(content,"\n")
f.close();
def main(start):
##initialize vars
id = int(start)
page = readFile(id)
soup = BeautifulSoup(page)
print soup.prettify()
readFile(id)
for id in range(1000000000):
parsedata()
id = id + 1
continue
main(sys.argv[1]);
While the HTML part im trying to scrape looks like this
<tr style="height:40px; background-color:#f0f0f0;"><td colspan="4" class="textLargeBold" style="border-bottom: solid 1px #c4c4c4;">Random Details</td></tr>
<tr class="text">
<td align="left" valign="top"><b>Type</b></td>
<td align="left" valign="top">Color</td>
<td align="left" valign="top"><b>Random Number</b></td>
<td align="left" valign="top">213523123123123</td>
</tr>
<tr class="text"
<td align="left" valign="top"><b>Random Month/Year</b></td>
<td align="left" valign="top">12/13</td>
<td align="left" valign="top"><b>Small Random Number</b></td>
<td align="left" valign="top">13233</td>
</tr>
I want the details that come after the first one. thus if I'm searching for Typem I want it to show me Color.
and ofcourse in the end I'd like the gotten contents to be parsed in a format similar to CSV.
Type;Random Number;Random Month/Year
it should parse
Color;213523123123123;12/13
ofcourse in the code i made already im not searching for type, but that can easily be changed.
EDIT : Fixed intendation
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)