这是我的第一篇文章,如果我违反了一些规则,请原谅我。我试图使用看起来像的代码来抓取供应商信息
soup.find_all('span', class_ = "class-name")
请参考所附图片。我想得到联系电话,但它不是以文本或类似的形式给出的。每个数字似乎都在它自己的类标签中,甚至在该数字不是文本中。我也不熟悉 webdev,所以如果有人能提出建议,我将不胜感激。
网址:https : //www.justdial.com/Pune/Sunrise-Enterprises-Budhwar-Peth/020PXX20-XX20-130817131104-Z3I2_BZDET? xid =UHVuZSBFbGVjdHJvbmljIENvbXBvbmvsC
另一个具有多个联系方式的类似页面是:https : //www.justdial.com/Pune/Galaxy-Enterprises-And-Electronics-Behind-Bharti-Vidyapeeth-Near-Ichapurti-Mandir-Ambegaon-Budruk/020PXX20-XX20-140930130951 -K4X6_BZDET?xid=UHVuZSBFbGVjdHJvbmljIENvbXBvbmVudCBEZWFsZXJz
谢谢
第二个样式标签包含 css 代码,其中icon-xx 属性的顺序定义了该属性与哪个数字匹配。这用于在网页上加载具有此编号的图像,因此没有要抓取的数字。解决方案是 1) 根据它们在 css 字符串中的顺序将 icon-xx 属性映射到数字;2)在html正文中找到电话号码范围并检索匹配的号码:
import requests
from bs4 import BeautifulSoup
url = 'https://www.justdial.com/Pune/Sunrise-Enterprises-Budhwar-Peth/020PXX20-XX20-130817131104-Z3I2_BZDET?xid=UHVuZSBFbGVjdHJvbmljIENvbXBvbmVudCBEZWFsZXJz'
r = requests.get(url, headers={'User-Agent' : "Mozilla/5.0 (Windows NT 6.1; Win64; x64)"})
soup = BeautifulSoup(r.text, "html.parser")
text = soup.find_all('style', {"type": "text/css"}, text=True)[1]
data = text.contents[0].split('smoothing:grayscale}', 1)[1].split('\n')[0]
icon_items = [i.split(':')[0] for i in data.split('.') if len(i)>0]
items = ['0','1','2','3','4','5','6','7','8','9','+','-',')','(']
full_list = dict(zip(icon_items, items))
phone_numbers = soup.find_all('span',{'class':'telnowpr'})
for i in phone_numbers:
numbers = i.find_all('span')
number = [full_list[y.attrs['class'][1]] for y in numbers]
print("phone number: " + ''.join([str(elem) for elem in number]) )
结果:
phone number: 07947197693
phone number: 07947197693
phone number: 07947197693
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句