在 Scraping python 中打印重复的电子邮件和电话号码

发布于 2025-01-11 12:12:37 字数 565 浏览 0 评论 0原文

我面临的问题是它打印两次电子邮件和电话号码,同时打印电话并发送到它如何修剪我尝试但失败的刮擦。 请帮助我摆脱这个困境。

import requests
from bs4 import BeautifulSoup
import pandas as pd
import time
suit=[]
url ="https://www.allenovery.com/en-gb/global/people/Shruti-Ajitsaria"

r= requests.get(url)

soup = BeautifulSoup(r.content, 'html.parser')
#time.sleep(1)
content = soup.find_all('div', class_ = 'hyperlinks')
#print(content)
for property in content:
  link = property.find('a', {'class': 'tel'})['href']
  email = property.find_next('a', {'class': 'mail'})['href']  
  print(link,email)

I am Facing issue it print two times email and phone number along with it print tele and send to also how it trim on scraping i tried but failed.
Please help me out from this.

import requests
from bs4 import BeautifulSoup
import pandas as pd
import time
suit=[]
url ="https://www.allenovery.com/en-gb/global/people/Shruti-Ajitsaria"

r= requests.get(url)

soup = BeautifulSoup(r.content, 'html.parser')
#time.sleep(1)
content = soup.find_all('div', class_ = 'hyperlinks')
#print(content)
for property in content:
  link = property.find('a', {'class': 'tel'})['href']
  email = property.find_next('a', {'class': 'mail'})['href']  
  print(link,email)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

野の 2025-01-18 12:12:37

在代码中使用它来查看循环将运行多少次
print(len(content))

如果这样做,您会发现循环的长度(等于 content 变量的长度)是 2,当您将 print(link,email) 放入循环中时,它将运行两次,并且您会看到打印结果两次......换句话说,您正在打印结果2次。要解决这个问题,请删除 print(link,email) 的缩进,将其放在循环之外,它将被修复。

Use this in your code to see that how many times your loop is going to be run
print(len(content))

If you do so, you will find that the length of your loop (which is equal to length of content variable) is 2, and as you put the print(link,email) inside your loop, it will run twice and you see the printed result two times... in other words, you are printing the result 2 times. to fix that, remove the indentation for print(link,email) to put it outside the loop and it will be fixed.

梦归所梦 2025-01-18 12:12:37

电子邮件和电话号码重复,因为它们多次存在。

import requests
from bs4 import BeautifulSoup
import pandas as pd
import time
suit=[]
url ="https://www.allenovery.com/en-gb/global/people/Shruti-Ajitsaria"

r= requests.get(url)

soup = BeautifulSoup(r.content, 'html.parser')
#time.sleep(1)

tel = soup.select('div.hyperlinks > ul > li > a')[0].get('href')
email= soup.select('div.hyperlinks > ul > li > a')[1].get('href')
print(tel,email)

输出:

tel:+44 20 3088 1831 mailto:[email protected]

Duplicate email and Telephone number because they exist more than once.

import requests
from bs4 import BeautifulSoup
import pandas as pd
import time
suit=[]
url ="https://www.allenovery.com/en-gb/global/people/Shruti-Ajitsaria"

r= requests.get(url)

soup = BeautifulSoup(r.content, 'html.parser')
#time.sleep(1)

tel = soup.select('div.hyperlinks > ul > li > a')[0].get('href')
email= soup.select('div.hyperlinks > ul > li > a')[1].get('href')
print(tel,email)

Output:

tel:+44 20 3088 1831 mailto:[email protected]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文