我是python的初学者,所以我想用beauthoulsoup来创建一个网站。页面源代码的一小部分是html:
<table class="swift" width="100%">
<tr>
<th class="no">ID</th>
<th>Bank or Institution</th>
<th>City</th>
<th class="branch">Branch</th>
<th>Swift Code</th>
</tr> <tr>
<td align="center">101</td>
<td>BANK LEUMI ROMANIA S.A.</td>
<td>CONSTANTA</td>
<td>(CONSTANTA BRANCH)</td>
<td align="center"><a href="/romania/dafbro22cta/">DAFBRO22CTA</a></td>
</tr>
<tr>
<td align="center">102</td>
<td>BANK LEUMI ROMANIA S.A.</td>
<td>ORADEA</td>
<td>(ORADEA BRANCH)</td>
<td align="center"><a href="/romania/dafbro22ora/">DAFBRO22ORA</a></td>
</tr>
我设法把它们刮了,但结果是:
^{pr2}$当我真的想要这样的时候:
ID, Bank or Institution, City, Branch, Swift Code
101, BANK LEUMI ROMANIA S.A., CONSTANTA, (CONSTANTA BRANCH) ,DAFBRO22CTA
102, BANK LEUMI ROMANIA S.A., ORADEA, (ORADEA BRANCH), DAFBRO22ORA
这是我的代码:
base_url = "https://www.theswiftcodes.com/"
nr = 0
page = 'page'
country = 'Romania'
while nr < 4:
url_country = base_url + country + '/' + 'page' + "/" + str(nr) + "/"
pages = requests.get(url_country)
soup = BeautifulSoup(pages.text, 'html.parser')
for script in soup.find_all('script'):
script.extract()
tabel = soup.find_all("table")
text = ("".join([p.get_text() for p in tabel]))
nr += 1
print(text)
file = open('swiftcodes.txt', 'a')
file.write(text)
file.close()
file = open('swiftcodes.txt', 'r')
for item in file:
print(item)
file.close()
这应该能起作用
输出:
^{pr2}$输出:
^{pr2}$相关问题 更多 >
编程相关推荐