Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中带有不需要的空行的刮表_Python_Web Scraping - Fatal编程技术网

python中带有不需要的空行的刮表

python中带有不需要的空行的刮表,python,web-scraping,Python,Web Scraping,我是python的初学者,在刮桌子时遇到了麻烦。我期望的目标是在输出中不要有任何空白。我的代码: import requests from bs4 import BeautifulSoup # I am only interested in some particular blocks from the bitcoin blockchain url = "https://blockchain.info/block-height/521578" # Getting table from the

我是python的初学者,在刮桌子时遇到了麻烦。我期望的目标是在输出中不要有任何空白。我的代码:

import requests
from bs4 import BeautifulSoup

# I am only interested in some particular blocks from the bitcoin blockchain
url = "https://blockchain.info/block-height/521578"

# Getting table from the url
response = requests.get(url)
soup = BeautifulSoup(response.content, "html.parser")
stat_table = soup.find_all('table', class_ = 'table table-striped')
stat_table = stat_table[0]

for row in stat_table.find_all("tr"):
    for cell in row.find_all("td")[1:]: # look that I am interested only in the 2nd column
        print(cell.text)
使用前面的代码,我得到以下结果:

521578 (Main chain)
0000000000000000002809c9ae7546964580751b506e070f15002b1c1fdd66b3

0000000000000000002d4f2f654945fda08931355e9af871a8c2135a25da9cb6
00000000000000000023583cc0df49783e50c93d807c405524168016c84c0c2a
2018-05-07 06:56:50

2018-05-07 06:56:50
BTC.com

4,022,059,196,164.95

390462291
452
5,440.06056147 BTC
233.87351861 BTC
342.123 KB
0x20000000
ee7c7e2cde5e0f3567c9f635549ec62365e2ac45da517f41cf6c32787c3d8b4d
3688672863
12.5 BTC
0.11100607 BTC
但当我想在以后保存.csv文件时,这些空行给我带来了麻烦。你知道如何去掉那些空行吗

如果我在代码中做了一个小的更改,我会得到我想要的数据,除了我不需要的列(我只需要第二列):

更改后的输出(看,没有空行,但我需要的列中没有空行):


请提前感谢

在打印前测试该值是否符合要求

for row in stat_table.find_all("tr"):
    for cell in row.find_all("td")[1:]:
        if cell.text != "":
            print(cell.text)

取出您在scraper中定义的
print
语句,并尝试用此
print(cell.get\u text(strip=True))替换它。
。谢谢SIM,这解决了问题。我尝试了使用条件,但不起作用。@SIM提到的注释完成了任务,即使用:
print(cell.get_text(strip=True))
Height
Hash
Previous Block
Next Blocks
Time
Received Time
Relayed By
Difficulty
Bits
Number Of Transactions
Output Total
Estimated Transaction Volume
Size
Version
Merkle Root
Nonce
Block Reward
Transaction Fees
for row in stat_table.find_all("tr"):
    for cell in row.find_all("td")[1:]:
        if cell.text != "":
            print(cell.text)