如何从html代码打印“打印我1”和“打印我2”?

时间:2018-12-22 03:46:12

标签: python-3.x web-scraping beautifulsoup

如果我有此HTML代码,

<div class="_1GGPkHIiaumnRMT-S1cU29"><span>print me 1</span><span><div class="_2ZBv5UiBzOiApuonYSpb92"><div>patates</div></div></span><span>print me 2</span></div>

如何获得“给我打印1”和“给我打印2”?

我正在使用BeautifulSoup在Python中学习网页抓取。

for subcat in category.find_all("div"):
        print(subcat)

1 个答案:

答案 0 :(得分:0)

逻辑

func updateRateTicker(json : JSON) {
    if let tempResult = json["results"]["MYR_JPY"]["val"].double {

        DispatchQueue.main.async {
            print(tempResult)
            rateLabel.text = String(tempResult)
        }
    }
}

代码

1) Encapsulate HTML in a single quoted string.
2) Initialize BeautifulSoup
3) Locate all Span tags where presumably only text will occur in between tags
4) Iterate across all returned values (strings) that have span in them
5) If div or any other tag occurs (other tag case not covered in answer) then ignore record
6) Otherwise print value, after removing span tags