[英]scraping a web page with python
這是代碼,它產生我想要的東西,但不是我想要的 output 結果
import requests
from bs4 import BeautifulSoup
url = 'https://en.wikipedia.org/wiki/2020_coronavirus_pandemic_in_Florida'
fl = requests.get(url)
fl_soup = BeautifulSoup(fl.text, 'html.parser')
block = fl_soup.findAll('td', {'class': 'bb-04em'})
for name in fl_soup.findAll('td', {'class': 'bb-04em'}):
print(name.text)
output
2020-04-21
27,869(+3.0%)
867
我想要這樣的 output 2020-04-21 27,869(+3.0%) 867
以下應該做你想要的:
import requests
from bs4 import BeautifulSoup
url = 'https://en.wikipedia.org/wiki/2020_coronavirus_pandemic_in_Florida'
fl = requests.get(url)
fl_soup = BeautifulSoup(fl.text, 'html.parser')
div_with_table = fl_soup.find('div', {'class': 'barbox tright'})
table = div_with_table.find('table')
for row in table.findAll('tr'):
for cell in row.findAll('td', {'class': 'bb-04em'}):
print(cell.text, end=' ')
print() # new line for each row
在訪問每個<td>
之前,嘗試通過每個<tr>
獲取數據,您將獲得每個表行的信息。 然后你可以在<td>
內搜索或任何你想要的。
對於最后一個打印語句,包括 end 參數。 默認情況下,打印語句有 end='\n'
print(name.text, end=' ')
這將為您提供所需的 output。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.