我用漂亮的汤库创建了一个twitter scraper。我已经成功地用一个用户的用户名检索到了该用户的个人简历和最上面的tweet。我唯一的问题是输出有点奇怪,因为输出是从HTML代码中提取的,它包含许多空行。你知道吗
我试过使用prettify,但结果都是空行。我也试过使用打印.pprint. 你知道吗
我是python新手,想不出任何其他方法可以让我的脚本输出更整洁
任何帮助都将不胜感激。你知道吗
下面是我的剧本:
import requests
from bs4 import BeautifulSoup
import pprint
q = "https://twitter.com"
def find_bio(username):
c = format("https://twitter.com"+"/" + username)
r = requests.get(c)
s = BeautifulSoup(r.text, "html.parser")
return s.find("div", class_="ProfileHeaderCard").text
def find_toptweet(username):
c = format("https://twitter.com"+"/" + username)
r = requests.get(c)
s = BeautifulSoup(r.text, "html.parser")
return s.find("div", class_="content").text
if __name__ == "__main__":
username = input('enter username: ')
bio = find_bio(username)
tweet = find_toptweet(username)
print("Bio--------------------------------------------------------------")
pprint.pprint(bio)
print("End of Bio-------------------------------------------------------")
print('top tweet')
pprint.pprint(tweet)
输出低于
enter username: altifali4
Bio--------------------------------------------------------------------------------------
('\n'
'\n'
'Altif Ali\n'
'\n'
'\n'
'\n'
'@AltifAli4\n'
'\n'
'\n'
'People, by and large, are good people\n'
'\n'
'UoH\n'
'\n'
'\n'
'\n'
'\n'
'\n'
'\n'
'\n'
' \n'
' instagram.com/altif.ali\n'
' \n'
'\n'
'\n'
'\n'
'\n'
'Joined August 2018\n'
'\n'
'\n'
'\n'
' Born 1999\n'
'\n'
'\n'
'\n')
End of Bio---------------------------------------------------------------- ----------------------
top tweet
('\n'
'\n'
'\n'
'\n'
'\n'
'Lowkey\u200f\xa0@Lowkey0nline\n'
'\n'
'May 22\n'
'\n'
'\n'
'\n'
'\n'
'\n'
'\n'
'More\n'
'\n'
'\n'
'\n'
'\n'
'\n'
'\n'
'\n'
'\n'
'\n'
'Copy link to Tweet\n'
'\n'
'\n'
'Embed Tweet\n'
'\n'
'\n'
'\n'
'\n'
'\n'
'\n'
'\n'
'Power concedes nothing without demand. Without demand power concedes '
'nothing.\n')
Process finished with exit code 0
尝试用以下语句替换
if
语句:希望这有帮助
相关问题 更多 >
编程相关推荐