这是我遇到问题的代码:
from bs4 import BeautifulSoup
import requests
url = requests.get('https://www.whatever.com/')
soup = BeautifulSoup(url.text, 'lxml')
scraper = soup.find_all('article', {'class' : 'article fc '})
for q in scraper:
result = q.find('div', {'class' : 'text-container'})
print((result.text[::-1]))
问题是反转的'result.text'输出。我只想要反转字符串部分,它不会触及整数。我怎样才能做到这一点?
谢谢。
您可以使用此代码来反转result.text
,如您解释的那样:
' '.join([x if x.isdigit() else x[::-1] for x in result.text.split()][::-1])
' '.join(map(lambda word: word if word.isdigit() else word[::-1], "cat 1234 1cat".split()))