【发布时间】:2020-10-15 20:56:28
【问题描述】:
所以我一直在尝试通过网络抓取公共网络目录https://www.kenyaplex.com/business-directory/?start=1&categoryid=286 以获取公司的联系方式。我的代码运行良好,但它没有在 br 标记之后选择信息,例如它没有在美丽汤的以下详细信息中选择电子邮件
<div class="c-detail">
<a href="https://www.kenyaplex.com/business-directory/86129-water-well-drilling-rig-gujarat-india.aspx">water well drilling rig, Gujarat, India</a><br/>+91-9825005407<br/>info@dhirajrigs.com</div>,
我试图更改列表的索引,但它不起作用,我附上了我所拥有的代码给我的图像。 我希望它同时选择电话号码和电子邮件,但它要么选择电话号码要么选择列出两者的电子邮件 我正在使用的代码如下
import requests
from bs4 import BeautifulSoup
import time
#DEFINING THE FIRST WEBPAGE
num = 1
#STRING FORMATTING THE URL TO CAPTURE DIFFRENT PAGES
url = 'https://www.kenyaplex.com/business-directory/?start={}&categoryid=286'.format(num)
#DEIFING THE BROWSER HEADERS SO THAT CAN WORK ON IT WITHOUT ERRORS
headers = {'User-Agent':'Chrome'}
records = []
while num < 100:
url = 'https://www.kenyaplex.com/business-directory/?start={}&categoryid=286'.format(num)
time.sleep(1)
num += 30
response = requests.get(url,headers)
soup = BeautifulSoup(response.text,'html.parser')
company_info = soup.find_all('div', attrs={'class':'c-detail'})
print(company_info)
#EXTRACTING SPECIFIC RECORDS
for name in school_info:
Name_of_The_Company = name.find('a').text
#Location_of_The_School = name.contents[2][2:]
Contact_of_The_Company = name.contents[3]
Email = name.contents[4]
#converting the records to a tuple
records.append((Name_of_The_Company,
#Location_of_The_School,
Contact_of_The_Company,
Email))
#EXPORTING TO A PANDAS FILE
import pandas as pd
df = pd.DataFrame(records, columns = ['Name of The Company',
#'Location of The School',
'Contact of The Company',
'Email'])
df.to_csv('boreholes.csv', index = False, encoding = 'utf-8')
【问题讨论】:
标签: python html pandas web-scraping beautifulsoup