【问题标题】:Webscraping using beautiful soup giving multiple resultsWeb Scraping 使用 beautifulsoup 给出多个结果
【发布时间】:2021-02-01 03:52:56
【问题描述】:

我正在抓取 [this][1] 页面,想要代理的手机号码。

我正在使用这个脚本

# phone num
        phonenum=soup.select(".phone-content")

        for a in phonenum:
            print a.text.strip().encode("utf-8")

但它会多次提供电话号码。我不知道为什么。我怎样才能将其限制为一个电话号码? [1]:http://dubai.dubizzle.com/property-for-rent/residential/apartmentflat/2014/2/2/do-you-want-to-live-in-amazing-views-of-bu-2-3/?back=ZHViYWkuZHViaXp6bGUuY29tL3Byb3BlcnR5LWZvci1yZW50L3Jlc2lkZW50aWFsL2FwYXJ0bWVudGZsYXQv&pos=1

【问题讨论】:

  • 请注意,两个电话内容类中有两个相同的电话号码。

标签: python beautifulsoup


【解决方案1】:

您正在抓取的页面有两个元素,其类为 phone-content。这就是为什么你会得到两个电话号码。您可以将更具体的 CSS 选择器传递给 .select 方法:

phonenum=soup.select("#listing-reply-controls .phone-content")

您也可以删除重复的电话号码:

phonenum=soup.select(".phone-content")
# set comprehension will remove the duplicate phone numbers
unique_phonenum = {p.text.strip().encode("utf-8") for p in phonenum}

for a in unique_phonenum:
    print a

【讨论】:

    猜你喜欢
    • 2021-09-03
    • 2018-04-02
    • 2020-12-18
    • 2021-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-21
    • 2015-12-20
    相关资源
    最近更新 更多