Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.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 BeautifulSoup返回额外的内容_Python_Parsing_Web Crawler_Bs4 - Fatal编程技术网

python BeautifulSoup返回额外的内容

python BeautifulSoup返回额外的内容,python,parsing,web-crawler,bs4,Python,Parsing,Web Crawler,Bs4,我正在尝试制作一个kickstarter承诺金额跟踪器,该跟踪器可以在不打印其他行的情况下更新打印金额 我遇到的问题是,它会打印附加字符以及抵押美元金额 我认为这与缩进有关,但我不确定 以下是打印的内容: (u'\n$116,954\n,) 它应该说: $116,954 在不添加新行的情况下更新为相同的打印 这是我的密码: import requests from bs4 import BeautifulSoup import time amount = '' url = raw_inp

我正在尝试制作一个kickstarter承诺金额跟踪器,该跟踪器可以在不打印其他行的情况下更新打印金额

我遇到的问题是,它会打印附加字符以及抵押美元金额

我认为这与缩进有关,但我不确定

以下是打印的内容:

(u'\n$116,954\n,)
它应该说:

$116,954
在不添加新行的情况下更新为相同的打印

这是我的密码:

import requests
from bs4 import BeautifulSoup
import time  
amount = ''
url = raw_input("What is the URL?")
def pageCrawler():
    global amount
    headers = {
        'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36'
    }
    res = requests.get(url, headers=headers)
    soup = BeautifulSoup(res.text, 'html.parser')
    data = soup.find_all(attrs={"class": "green-700 inline-block js-usd_pledged medium type-16 type-24-md"})
    result = data[0].text
    if amount != result :
        amount = result
        print(amount,)
        time.sleep(1)


while 1:
    pageCrawler()
    time.sleep(10)  #check amount every 10 seconds

为了简洁起见,我删除了通知音和(int转换)菜单,在此代码中选择kickstarter、youtube或instructables选项。

可能该部分在该数量前后都包含换行符。为什么您不能将其转换为
result=data[0].text.strip()
?这是一个Unicode字符串。解码它,它应该是正常的。我尝试使用unidecode和.strip()进行解码,但没有任何改进<代码>从unidecode导入unidecode结果=unidecode(结果)结果=结果。条带(“”)确定。因此,删除
print(amount,)
中的逗号可以解决额外字符的问题。但是,现在它不会更新现有的打印行。它只是在更新新内容时打印到新行。如何解决此问题?
print(chr(27)+“[2J”)
在您的print语句将清除终端之前(对于大多数终端)。来源:stackoverflow.com/a/208452