实际上,我想从一个网站"https://www.crunchbase.com/organization/ani-technologies#/entity“中抓取数据,其中我的数据存在于dt和dd标签中,并且由于bot在网站上是不允许的。因此,我以这种方式保存了页面,并在保存的页面上应用了美观汤模块,尽管我在代码中提到了实际的url。
soup = BeautifulSoup(open(r"C:\Users\acer\Desktop\pythonbooks\tam.html").read())import requests
ctr=1
file=requests.get("https://www.crunchbase.com/organization/ani-technologies#/entity")
soup = BeautifulSoup(file).read()
dl_data = soup.find_all("dd")
for dlitem in dl_data:
print(ctr,dlitem.string)
ctr+=1实际产出: 0 3收购1无2孟加拉,卡纳塔克3 Ola是一个移动应用程序出租车预订在印度。无5无6 olacab连接7无2010年12月3日9 ANI技术有限公司,Olacabs.com,Ola Cabs,Olacabs 10 media@olacabs.com 11无
在几个地方,我没有得到任何的事实,因为有超链接到contents.for,例如,在页面"https://www.crunchbase.com/organization/ani-technologies#/entity“选项卡有5个类别名为:电子商务,互联网,交通,应用程序和移动,每一个是连接到一个超链接,所以我无法得到我想要的文本,即这5个类别。
作为输出我想要的是: 0 3收购1(所有的文本(尽管对我来说并不重要)2孟加拉,卡纳塔克3 Ola是一个移动应用程序出租车预订在印度。==>5 (电子商务、互联网、运输、应用程序、移动)(非常重要)6 olacab链接7(所有文本(尽管对我来说不重要))2010年12月3日9 ANI科技有限公司,Olacabs.com,Ola Cabs,Olacabs 10 media@olacabs.com 11 (所有这些文本(虽然对我不重要))
如果我能得到这样的字典,那将是非常有帮助的:
{"Headquarters":["Bengaluru,Karnataka"],
"Description":["Ola is a mobile app for cab booking in India."],
"Category": ["E-Commerce", "Internet", "Transportation", "Apps", "Mobile"]}发布于 2017-07-06 19:50:41
问题:我得不到我想要的短信..。如果我能拿到字典..。
将text/href从所有<dd><a href=...>text</dd>中获取,以聚合到dict中,例如:
from collections import OrderedDict
os_dict = OrderedDict()
for div_class in ['definition-list-container', 'details definition-list']:
divs = soup.find_all("div", class_=div_class)
key = '?'
for div in divs:
for child in div.findChildren():
if child.name == 'dt':
key = child.text[:-1]
if child.name == 'dd':
if child.select('a[href]'):
a_list = child.find_all("a")
if key in ['Social:']:
os_dict[key] = [a['href'] for a in a_list]
elif len(a_list) == 1:
os_dict[key] = a_list[0].text
else:
os_dict[key] = [a.text for a in a_list]
else:
os_dict[key] = child.text
for n, key in enumerate(os_dict, 1):
print('{:>2}: {:>20}:\t{}'.format(n, key, os_dict[key]))Outuput 1:收购:3宗收购-- 2:总股本基金:'11轮‘,'24投资者’3:总部:本加鲁,卡纳塔克4:描述: Ola是一款用于在印度预订出租车的移动应用。5:创始人:'Bhavish Aggarwal','Ankit Bhati‘6:类别:’电子商务‘,’互联网‘,’运输‘,’应用‘,’移动‘7:网站:http://www.olacabs.com 8:社交:'http://twitter.com/olacabs',’'http://www.linkedin.com/company/olacabs-com‘9:成立日期:2010年12月3日10:别名: ANI Technologies Pvt Ltd,Olacabs.com,Ola Cabs,Olacabs 11:联系人: media@olacabs.com 12:员工:8在Crunchbase
漂亮的汤文档:找到-全部 签名:find_all(名称、吸引、递归、字符串、限制、**kwargs)
dl_data = soup.find_all("dd")
for n, dlitem in enumerate(dl_data, 1):
if dlitem.select('a[href]'):
a_text = [a.text for a in dlitem.find_all("a")]
print('{}: {}'.format(n, a_text))
else:
print('{}: {}'.format(n, dlitem.text))用Python:3.4.2-BS 4:4.6.0测试的
https://stackoverflow.com/questions/44930665
复制相似问题