import requests
from bs4 import BeautifulSoup
url = 'https://xueqiu.com/ask/square'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3377.1 Safari/537.36',
}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'lxml')
div_tags = soup.select('div.ask-list.active > div')
for div_tag in div_tags:
ask = ''.join(div_tag.select('p')[0].text.split(' ')[1:])
answer = div_tag.select('div > div > p')[0].text
print('问:' + ask)
print('答:' + answer)