2017-09-18 103 views
1

我想從網站爬取數據,但問題是有負載更多的按鈕來查看下50個記錄,同樣的方式,我必須點擊,直到記錄結束。使用蟒蛇和硒爬行網站

我只能夠獲取50個名稱和地址。需要獲取所有直到加載更多的結束。

動態點擊按鈕我使用硒與python。

我想找到所有的零售商城市的名稱,地址和電話號碼明智

我嘗試:

import time 
from bs4 import BeautifulSoup 
from selenium import webdriver 
from selenium.common.exceptions import TimeoutException 

url = "https://www.test.in/chemists/medical-store/gujarat/surat" 
browser = webdriver.Chrome() 
browser.get(url) 

time.sleep(1) 
html = browser.page_source 
soup = BeautifulSoup(html, "lxml") 

try: 
    for row in soup.find_all("div", {"class":"listing "}): 
     #print(row.get_text()) 
     name = row.h3.a.string 
     address = row.p.get_text() 
     #contactnumber = need to find (can view after click on retailer name) 
     print(name) 
     print(address) 
     print(contactnumber) 

    button = browser.find_element_by_id("loadmore") 
    button.click() 

except TimeoutException as ex: 
    isrunning = 0 

#browser.close() 
#browser.quit() 

回答

1

如果您檢查,當你打load more你,是由網絡電話可以看到其參數是城市,州和頁碼的發佈請求。因此,不是在selnium中加載腳本,而是使用普通的請求模塊來完成。例如,這個函數會在遍歷頁面時爲您執行加載更多功能。

def hitter(page): 
    url = "https://www.healthfrog.in/importlisting.html" 

    payload = "page="+str(page)+"&mcatid=chemists&keyword=medical-store&state=gujarat&city=surat" 
    headers = { 
     'content-type': "application/x-www-form-urlencoded", 
     'connection': "keep-alive", 
     'cache-control': "no-cache", 
     'postman-token': "d4baf979-343a-46e6-f53b-2f003d04da82" 
    } 

    response = requests.request("POST", url, data=payload, headers=headers) 
    return response.text 

上述函數爲您提取包含名稱和地址的頁面的html。現在您可以遍歷頁面,直到找到不返回內容的頁面。例如,如果嘗試與卡納塔克邦和邁索爾的城市,你會發現第三和第四頁之間的區別。這會告訴你你必須停止的地方。

要獲取電話號碼,您可以請求批量上市響應(以前的響應)的<h3>標籤中的html。示例html:

<div class="listing"> 
    <h3> 
     <a href="https://www.healthfrog.in/chemists/sunny-medical-store-surat-v8vcr3alr.html">Sunny Medical Store</a> 
    </h3> 
    <p> 
     <i class="fa fa-map-marker"></i>155 - Shiv Shakti Society, Punagam, , Surat, Gujarat- 394210,India 
    </p> 
</div> 

您將需要解析html並找出電話號碼在哪裏,然後您可以填充它。您可以使用要求的例子:

html = requests.get('https://www.healthfrog.in/chemists/sunny-medical-store-surat-v8vcr3alr.html').text 

現在,您可以beautifulSoup解析HTML像你前面做的。

用請求代替硒做這件事有很多好處,每次你需要一個電話號碼時你不需要打開和關閉多個窗口,並且你可以避免每次點擊加載時都會過期。它也快得多。


請注意:如果您是這樣做的,請遵守網站設置的規則。不要通過發送太多請求來使其崩潰。

編輯:工作刮刀。

import requests, time, re 
from bs4 import BeautifulSoup 

def hitter(page, state="Gujarat", city="Surat"): 
    url = "https://www.healthfrog.in/importlisting.html" 

    payload = "page="+str(page)+"&mcatid=chemists&keyword=medical-store&state="+state+"&city="+city 
    headers = { 
     'content-type': "application/x-www-form-urlencoded", 
     'connection': "keep-alive", 
     'cache-control': "no-cache" 
    } 

    response = requests.request("POST", url, data=payload, headers=headers) 
    return response.text 

def getPhoneNo(link): 
    time.sleep(3) 
    soup1 = BeautifulSoup(requests.get(link).text, "html.parser") 
    f = soup1.find('i', class_='fa fa-mobile').next_element 
    try: 
     phone = re.search(r'(\d{10})', f).group(1) 
    except AttributeError: 
     phone = None 
    return phone 

def getChemists(soup): 
    stores = [] 
    for row in soup.find_all("div", {"class":"listing"}): 
     print(row) 
     dummy = { 
      'name': row.h3.string, 
      'address': row.p.get_text(), 
      'phone': getPhoneNo(row.h3.a.get_attribute_list('href')[0]) 
     } 
     print(dummy) 
     stores.append(dummy) 

    return stores 

if __name__ == '__main__': 
    page, chemists = 1, [] 
    city, state = 'Gulbarga', 'Karnataka' 
    html = hitter(page, state, city) 
    condition = not re.match(r'\A\s*\Z', html) 
    while(condition): 
     soup = BeautifulSoup(html, 'html.parser') 
     chemists += getChemists(soup) 
     page += 1 
     html = hitter(page, state, city) 
     condition = not re.match(r'\A\s*\Z', html) 
    print(chemists) 
+0

你的腳本里有什麼新東西?我的腳本也做同樣的事情,你的腳本也只獲取50行,問題是loadmore按鈕多數民衆贊成爲什麼我用硒來爬行整個 –

+0

當你可以打一個簡單的捲曲時不要使用硒。一次只處理一頁。處理,直到頁面的響應爲空 – TheChetan

+0

,但你的腳本只返回50條記錄,如果你會看到有加載的按鈕,如何處理? –