我怎样才能得到唯一的姓名和联系电话?

2024-05-23 13:31:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从div那里得到姓名和联系电话。div有时有一个跨度,有时有两个跨度,有时有三个跨度。我的期望是:

  • 我只需要姓名和联系电话,如果有的话
  • 在某些情况下,姓名将不可用,联系人号码将可用,则名称变量应分配为“N/A”
  • 在某些情况下,联系人号码和姓名将不可用,然后变量应分配为“N/A”

到目前为止,我的情况是:

// if you change url to url-1 and url-2 then you will see how it works.
url = "https://www.zillow.com/homedetails/19442-185th-Ave-SE-Renton-WA- 
98058/54831221_zpid/"
#url-1 = "https://www.zillow.com/homedetails/20713-61st-St-E-Bonney-Lake-WA-98391/99371104_zpid/"
#url-2 = "https://www.zillow.com/homes/fsbo/house_type/121319389_zpid/globalrelevanceex_sort/47.465758,-122.259207,47.404798,-122.398424_rect/12_zm/5f9305c92cX1-CRbri51bo8epha_yly1g_crid/0_mmm/"
browser = webdriver.Firefox()
browser.get(url)
time.sleep(5)

soup = bs4.BeautifulSoup(browser.page_source,'html.parser')

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)

print(contact_phone) // Output: (253) 335-8690
print(contact_name)  // Output: ['Sheetal Datta']

Tags: namehttpsbrowsercomurlbywww情况
2条回答

欢迎来到StackOverflow!您应该以编程的方式来处理这个问题,即使用条件。你已经说过了

if the name exists and the contact number exists,
    use them
else if the name exists only,
    use the name and assign the contact number as 'N/A'
else if the contact number exists only,
    use the contact number and assign the name as 'N/A'

如您所见,可以使用if-elif-else语句在Python中将上述伪代码实现为实际的条件语句。根据网页的结构,在尝试从中读取值之前,需要先检查span的值是否存在,这可以在SO post之后执行。你知道吗

您可以使用try: except:检查联系人姓名和电话号码是否存在,然后相应地赋值。查看代码。。。你知道吗

from bs4 import BeautifulSoup
from selenium import webdriver
import time

url = ('https://www.zillow.com/homedetails/19442-185th-Ave-SE-Renton-WA-'
'98058/54831221_zpid/')

browser = webdriver.Firefox()
browser.get(url)
time.sleep(5)
soup = BeautifulSoup(browser.page_source,'html.parser')
browser.quit()
tag = soup.find('div',attrs={
    'class':'home-details-listing-provided-by zsg-content-section'})

try:
    contact_name = tag.find('span',attrs={
        'class':'listing-field'}).text
except:
    contact_name = 'N/A'

try:
    contact_phone = tag.find('span',attrs={
        'class':'listing-field'}).findNext('span').text
except:
    contact_phone = 'N/A'


print('Contact Name: {}\nContact Phone: {}'.format(
    contact_name,contact_phone))

输出:

Contact Name: Sheetal Datta
Contact Phone: (253) 335-8690

相关问题 更多 >