【发布时间】:2020-10-14 03:27:40
【问题描述】:
我有一本字典,其中包含公司名称以及该公司随附的相关商业改进局链接。我还有一个 CSV 文件,其中有 BBB 链接附加到这些公司的电话号码。我需要根据与公司名称关联的 BBB 链接以某种方式将两者结合起来。
我的最终目标是拥有一个包含以下内容的数据框:
公司名称、链接、电话号码
字典:
{'A. G. Builders, Inc.': 'https://www.bbb.org/us/nc/durham/profile/home-builders/ag-builders-inc-0593-6037923', 'A. R. Russell': 'https://www.bbb.org/us/nc/raleigh/profile/general-contractor/russell-l-judy-builder-inc-0593-90082691', 'A. R. Russell Builders, Inc.': 'https://www.bbb.org/us/nc/raleigh/profile/general-contractor/russell-l-judy-builder-inc-0593-90082691', 'A.C.A. Enterprises, LLC': 'https://www.bbb.org/us/fl/ponce-de-leon/profile/building-contractors/aca-enterprises-llc-0683-90029401', 'A.D. Myers Builders, LLC': 'https://www.bbb.org/us/nc/charlotte/profile/general-contractor/meyer-builders-llc-0473-219405', 'ABS Construction Group': 'https://www.bbb.org/us/nc/newport/profile/general-contractor/ab-building-remodeling-llc-0593-90293532', 'Absolute Construction Group, LLC': 'https://www.bbb.org/us/nc/durham/profile/home-improvement/absolute-construction-group-llc-0593-90282628'}
代码:
phone_list = []
url_with_phone = []
def phone_numbers():
driver = webdriver.Chrome()
for url in url_list: #Looping through the list of the BBB links
print(url) #Print the URL currently on
driver.get(url)
sleep(randint(4,6))
phone = driver.find_elements_by_class_name("dtm-phone") #FINDS Phone num
sleep(randint(4,8))
print('looking for number')
for p in phone:
results = (p.text)
print(results)
sleep(randint(3,5))
phone_list.append(results) # add phone number to phone_list
sleep(randint(5,9))
url_with_phone.append(url) #adds URL when phone num is found to match up with phone num
phone_numbers()
链接和电话号码的 CSV 输出:
URL Searched,Phone Numbers
https://www.bbb.org/us/nc/durham/profile/home-builders/ag-builders-inc-0593-6037923,(919) 384-7005
https://www.bbb.org/us/nc/raleigh/profile/general-contractor/russell-l-judy-builder-inc-0593-90082691,(919) 625-7841
https://www.bbb.org/us/nc/raleigh/profile/general-contractor/russell-l-judy-builder-inc-0593-90082691,(919) 625-7841
https://www.bbb.org/us/fl/ponce-de-leon/profile/building-contractors/aca-enterprises-llc-0683-90029401,(850) 248-0597
https://www.bbb.org/us/fl/ponce-de-leon/profile/building-contractors/aca-enterprises-llc-0683-90029401,(850) 527-1767
https://www.bbb.org/us/nc/charlotte/profile/general-contractor/meyer-builders-llc-0473-219405,(704) 737-8409
例如,CSV 文件中的第一个结果属于 AG Home Builders,有没有办法可以根据匹配值将字典(公司名称)中的键添加到 CSV?
我想将公司名称添加到 CSV。最好的方法是什么?我已阅读以下链接以尝试得出我自己的结果,但我自己尝试解决方案没有任何运气。 (append multiple values for one key in a dictionary, list to dictionary conversion with multiple values per key?)
【问题讨论】:
-
aca-enterprises-llc是公司名称吗? -
是的。有什么具体的我应该注意的吗?
-
名称中应该包含数字吗?
-
不应包含网络链接中的数字。名称应与字典中的名称相匹配。例如:'A.C.A. Enterprises, LLC': 'bbb.org/us/fl/ponce-de-leon/profile/building-contractors/…' 将有两个条目 [(850) 248-0597, (850) 527-1767 ] 这两个数字都属于 ACA Enterprises LLC
-
@AnnZen,是的,它似乎会起作用!我认为从链接中提取名称是一种选择。
标签: python pandas csv dictionary k