2017-10-14 55 views
0

我是python的新手。试图从零开始学习......但需要做一些事情......这意味着我还没有完成我的阅读。Python beautifulsoup得到2行文字

我有下面的代码

import requests 
from bs4 import BeautifulSoup 

url="https://www.xxx.co.uk" 
page=requests.get(url) 
soup = BeautifulSoup(page.content, 'html.parser') 

lotnav=soup.find(id="lotnav") 
address=lotnav.find(class_="col-sm-18").find_all("b") 
timeofauction=lotnav.find(class_="col-sm-18").select("span")[1].get_text() 

dateofauction=lotnav.find(class_="col-sm-18") 

内dateofauction的文字是

XXXX | 
14:00, 
         05 December 2017 

              63 Mattocke XXX, XXXxxxx, XX1 1XX 

我很努力,选择 “2017年12月05日” 到一个变量。 你能帮忙吗?

感谢 阿米特

+0

发布初始html – RomanPerekhrest

+0

是否在你的结果中断行相关且一致?如果是这样,然后拆分结果并获得相关的行。例如'lines = dateofauction.split()'和'result = lines [2]' –

回答

0

如果this是你想刮页面,则可以看出,col-sm-18divclass值包含显示的日期后两个span标签。紧接在日期之前的span包含时间,并且由于您已经在代码中引用它,因此您可以调用其上的nextSibling方法。

lotnav.find(class_="col-sm-18").select("span")[1].nextSibling.strip() 

这会给你:

05 December 2017 

注:nextSibling方法返回一个NavigableString对象,所以你不应该调用一个get_text()方法就可以了。它会引发错误。模拟地剥去它的空白并使用它。