溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

爬取某數(shù)據(jù)公司電話(huà)以及地址信息的方法

發(fā)布時(shí)間:2020-07-03 15:23:20 來(lái)源:億速云 閱讀:444 作者:清晨 欄目:編程語(yǔ)言

這篇文章將為大家詳細(xì)講解有關(guān)爬取某數(shù)據(jù)公司電話(huà)以及地址信息的方法,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

準(zhǔn)備:selenium+PhatomJS或者selenium+Firefox

我這里直接用的后者selenium+Firefox

思路: 爬取這部分信息的話(huà),代碼其實(shí)不難,主要包括模擬登陸、獲得頁(yè)面網(wǎng)址以及抓取頁(yè)面信息。

模擬登陸

網(wǎng)址:https://www.tianyancha.com/login

頁(yè)面如下:

爬取某數(shù)據(jù)公司電話(huà)以及地址信息的方法

使用selenium模擬登陸代碼:

time.sleep(random.random()+1)
browser.get(loginURL)
time.sleep(random.random()+random.randint(2,3))
browser.find_element_by_css_selector('div.title:nth-child(2)').click()
time.sleep(random.uniform(0.5,1))
phone = browser.find_element_by_css_selector('div.modulein:nth-child(2) > div:nth-child(2) > input:nth-child(1)')
phone.send_keys(zhangHao)
time.sleep(random.uniform(0.4,0.9))
password = browser.find_element_by_css_selector('.input-pwd')
password.send_keys(miMa)
click = browser.find_element_by_css_selector('div.modulein:nth-child(2) > div:nth-child(5)')
click.click()
time.sleep(random.uniform(0.5,1)+10)

登錄之后頁(yè)面:

爬取某數(shù)據(jù)公司電話(huà)以及地址信息的方法

關(guān)鍵詞對(duì)應(yīng)的頁(yè)面網(wǎng)址:https://www.tianyancha.com/search?key= + key
這里以“滴滴”為例:https://www.tianyancha.com/search?key=滴滴
頁(yè)面內(nèi)容如下:

爬取某數(shù)據(jù)公司電話(huà)以及地址信息的方法

獲取公司頁(yè)面網(wǎng)址
解析滴滴關(guān)鍵詞頁(yè)面HTML,獲得每個(gè)公司對(duì)應(yīng)得URL。
注意:非會(huì)員只能查看前五頁(yè)的公司信息
代碼:

#獲取頁(yè)面數(shù)
try:
    pages = soup.find('ul',class_='pagination').find_all('li')[-2].getText().replace('...','')
except:
    pages = 1
finally:
    print('pages:',pages)

def getUid(soup):
    urls = []
    divs = soup.find('div',class_='result-list sv-search-container').find_all('div',class_='search-item sv-search-company')                                                                             
    for div in divs:
        urls.append(div.find('div',class_='header').find('a')['href'])
    return urls

#非會(huì)員只能爬前五頁(yè)
if(int(pages)>5):
    pages = 5

urls = []
for i in range(1,pages+1):
    url = 'https://www.tianyancha.com/search/p' + str(i) + '?key=' + key
    browser.get(url)
    time.sleep(random.uniform(0.6,1)+2)
    soup = BeautifulSoup(browser.page_source,'lxml')
    urls.extend(getUid(soup))

獲得企業(yè)信息
最后根據(jù)企業(yè)網(wǎng)頁(yè)HTML內(nèi)容,解析獲取需要的信息,看頁(yè)面可以發(fā)現(xiàn)這里需要的電話(huà)和地址都在最上面就有。

爬取某數(shù)據(jù)公司電話(huà)以及地址信息的方法

獲取這部分內(nèi)容代碼:

#這里為了避免意外每次將結(jié)果直接寫(xiě)入Excel文件
try:
    for url in urls:
        path = r'C:\Users\liuliang_i\Desktop\tianYanCha.xlsx'
        try:
            df1 = pd.read_excel(path)
        except:
            df1 = pd.DataFrame(columns=['Company','Phone','Address','Url'])
        browser.get(url)
        time.sleep(random.uniform(0.4,0.8)+1)
        soup = BeautifulSoup(browser.page_source,'lxml')
        company = soup.find('div',class_='header').find('h2',class_='name').getText()
        phone = soup.find('div',class_='in-block sup-ie-company-header-child-1').find_all('span')[1].getText()
        address = soup.find('div',class_='auto-folder').find('div').getText()
        df1.loc[df1.shape[0],'Company'] = company
        df1.loc[df1.shape[0]-1,'Phone'] = phone
        df1.loc[df1.shape[0]-1,'Address'] = address
        df1.loc[df1.shape[0]-1,'Url'] = url
        df1.to_excel(path,index=0)
except :
    pass

最終結(jié)果如下所示:爬取某數(shù)據(jù)公司電話(huà)以及地址信息的方法

關(guān)于爬取某數(shù)據(jù)公司電話(huà)以及地址信息的方法就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI