我正在尝试抓取此站点链接。我正在尝试抓取这个特定的部分,请在下面找到 HTML:
<div style="padding:20px;">
<h1>
ABDULLA SALEM CONTRACTING EST
</h1>
<strong>
<a href="directory/umm-al-quwain/umm-al-quwain/building-contractors.html" title="Building
Contractors in Umm Al Quwain">
Building Contractors
</a>
</strong>
<br> P.O. Box: 200
<br> Location: Umm Al Quwain
<br> Phone: 06-7655445
</div>
import requests
import re
import csv
from bs4 import BeautifulSoup
def comp_links():
url=requests.get("https://www.uae-business-directory.com/directory/umm-al-quwain/umm-al-quwain/building-contractors.html").text
soup=BeautifulSoup(url,'lxml')
links=soup.find_all('a', attrs={'href': re.compile("^directory/umm-al-quwain/umm-al-quwain/building-contractors/")})
return links
def comp_details(z):
filename='comp.csv'
f=open(filename,'w')
music=csv.writer(f)
a=[]
def email_format():
if 'E-Mail' in details.text:
mail=details.img['src']
email=mail.replace('typo3temp/GB/','').replace('%40','@').split('_')[0]
return email
for i in z:
comp=requests.get('https://www.uae-business-directory.com/'+i['href']).text
soup_comp=BeautifulSoup(comp,'lxml')
details=soup_comp.find('div',class_='details')
for i in details:
print(i.text)
music.writerow([i.get_text(),email_format()]) #Writing to CSV
z=comp_links()
comp_details(z)
输出是这样的:
ABDULLA SALEM CONTRACTING ESTBuilding ContractorsP.O. Box: 200Location: Umm Al Quwain电话:06-7655445
我怎样才能得到它:
- 阿布杜拉塞勒姆合同 EST
- 建筑承包商
- 邮政信箱:200
- 地点:乌姆盖万
- 电话:06-7655445