【问题标题】:How can i get only name and contact number from div?如何从 div 中仅获取姓名和联系电话?
【发布时间】:2019-04-09 04:38:09
【问题描述】:

我正在尝试从 div 获取姓名和联系电话,而 div 有三个跨度,但问题是有时 div 只有一个跨度,有时是两个跨度,有时是三个跨度。

  • 第一个跨度有名字。

  • 第二个跨度有其他数据。

  • 第三跨度有联系电话

这里是 HTML

<div class="ds-body-small" id="yui_3_18_1_1_1554645615890_3864">
 <span class="listing-field" id="yui_3_18_1_1_1554645615890_3863">beth 
 budinich</span>
 <span class="listing-field"><a href="http://Www.redfin.com" 
 target="_blank">See listing website</a></span>
 <span class="listing-field" id="yui_3_18_1_1_1554645615890_4443">(206) 
 793-8336</span>
</div>

这是我的代码

try:
  name= browser.find_element_by_xpath("//span[@class='listing-field'][1]")
  name = name.text.strip()
  print("name : " + name)
except:
  print("Name are missing")
  name = "N/A"

try:
  contact_info= browser.find_element_by_xpath("//span[@class='listing- 
  field'][3]")
  contact_info = contact_info.text.strip()
  print("contact info : " + contact_info)
except:
  print("contact_info are missing")
  days = "N/A" 

我的代码没有给我正确的结果。谁能给我最好的解决方案。谢谢

【问题讨论】:

  • 我的代码没有给我正确的结果向我们展示结果,并解释为什么它们不是你想要的。
  • @JohnGordon 如果 div 有前两个跨度,那么第二个分配给联系号码。如果 div 有 1,2 或 3 跨度,你能给我正确的代码吗?

标签: python selenium web-scraping


【解决方案1】:

您可以迭代 throw 联系人并检查是否有子 a 元素以及是否匹配电话号码模式:

contacts = browser.find_elements_by_css_selector("span.listing-field")

contact_name = []
contact_phone = "N/A"
contact_web = "N/A"

for i in range(0, len(contacts)):
    if len(contacts[i].find_elements_by_tag_name("a")) > 0:
        contact_web = contacts[i].find_element_by_tag_name("a").get_attribute("href")
    elif re.search("\\(\\d+\\)\\s+\\d+-\\d+", contacts[i].text):
        contact_phone = contacts[i].text
    else:
        contact_name.append(contacts[i].text)

contact_name = ", ".join(contact_name) if len(contact_name) > 0 else "N/A"

输出:

contact_name:['凯文霍华德','霍华德企业']
contact_phone: '(206) 334-8414'

页面有验证码。为了更好地抓取使用,所有信息以json格式提供。

【讨论】:

  • #xpath 代码给我错误。
  • 这不是错误,WebElement 返回。检查更新的代码,添加.text 以获取文本
  • 我仍然面临问题,如果 div 只有跨度,那么第一个和最后一个跨度文本将是相同的。如果 div 有一个跨度,那么其他变量应该为空。
  • 这一行有语法错误:contact_phone = contacts[i] if re.search("\(\\d+\)\\s+\\d+-\\d+", contacts[ i])
  • 固定:contact_phone = contacts[i].text
【解决方案2】:
#sudharsan
# April 07 2019
from bs4 import BeautifulSoup
text ='''<div class="ds-body-small" id="yui_3_18_1_1_1554645615890_3864">
<span class="listing-field" id="yui_3_18_1_1_1554645615890_3863">beth 
budinich</span>
<span class="listing-field"><a href="http://Www.redfin.com" 
target="_blank">See listing website</a></span>
<span class="listing-field" id="yui_3_18_1_1_1554645615890_4443">(206) 
793-8336</span>
</div>'''
# the given sample html is stored as a input in variable called "text"
soup = BeautifulSoup(text,"html.parser")
main = soup.find(class_="listing-field")
# Now the spans with class name "listing-field" is stored as list in "main"
print main[0].text
# it will print the first span element
print main[-1].text
# it will print the last span element
#Thank you
# if you like the code "Vote for it"

【讨论】:

  • 代码看起来不错,可以用 Python Selenium 重写吗?因为我使用的是 selenium 浏览器
  • 感谢您的评论,实际上您也可以在 selenium 中使用上述代码,方法是使用 selenium 选项“driver.page_source”抓取 html 内容并将其存储在“text”变量而不是该示例 html文本。其余代码与抓取类似
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-12
  • 1970-01-01
  • 1970-01-01
  • 2012-10-11
  • 1970-01-01
  • 2015-06-01
  • 2014-02-12
相关资源
最近更新 更多