使用 Beautifulsoup 4 进行网页抓取 - 提取联系信息

老套

html代码截图

这是我的第一篇文章,如果我违反了一些规则,请原谅我。我试图使用看起来像的代码来抓取供应商信息

  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

谢谢

RJ阿德里安森

第二个样式标签包含 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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用Beautifulsoup 4进行Webscrapping-提取联系信息

使用 BeautifulSoup 提取抓取网页的 Script 标签所需的信息

使用 BeautifulSoup 进行网页抓取 / Zomato 网页抓取

使用 BeautifulSoup FindAll 进行网页抓取

使用 Python BeautifulSoup 进行网页抓取

使用BeautifulSoup进行网页抓取时出错

如何使用 BeautifulSoup 进行网页抓取

使用 BeautifulSoup 和 json 进行网页抓取

使用 BeautifulSoup 进行网页抓取 -- Python

使用 beautifulsoup 进行网页抓取的问题

使用 BeautifulSoup 遍历 URL 以进行网页抓取

使用beautifulsoup进行Python网页抓取-无法从Clinicaltrials.gov提取首席调查员

使用 bs4 进行网页抓取

使用BeautifulSoup抓取网页

使用 BeautifulSoup 抓取网页

使用BeautifulSoup进行网页抓取将不起作用

使用BeautifulSoup进行网页抓取时出现属性错误

在 python 网页抓取中使用 Selenium 对 BeautifulSoup 进行分页

使用 Selenium 和 BeautifulSoup 进行 Zillow 网页抓取

使用 Selenium 和 BeautifulSoup 进行网页抓取返回空列表

使用 BeautifulSoup 进行网页抓取,在 html 中找不到表格

使用BeautifulSoup Python抓取网页

使用 Beautifulsoup 抓取 UEFA 网页

使用BeautifulSoup4进行数据抓取的问题

使用python 2.7和beautifulsoup 4进行网站抓取

使用 python 和 bs4 进行网页抓取

使用 BS4 进行网页抓取:无法获取表格

使用 bs4 进行网页抓取不返回数值

用beautifulsoup进行网页抓取