在 Scraping python 中打印重复的电子邮件和电话号码
我面临的问题是它打印两次电子邮件和电话号码,同时打印电话并发送到它如何修剪我尝试但失败的刮擦。 请帮助我摆脱这个困境。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在代码中使用它来查看循环将运行多少次
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) is2
, and as you put theprint(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 forprint(link,email)
to put it outside the loop and it will be fixed.电子邮件和电话号码重复,因为它们多次存在。
输出:
Duplicate email and Telephone number because they exist more than once.
Output: