2017-05-16 2 views
0

NY 주식이 포함 된 웹 사이트를 탐색하려고합니다.파이썬 웹 스크래핑

https://markets.ft.com/data/indices/tearsheet/constituents?s=NYA:PSE

각 페이지는 20 개 주식 정보를 포함하고 95 페이지가있다. HTML 코드는 처음 20 개 정보 만 포함합니다. 어떻게 든 탐색하고 다음 페이지로 이동할 수 있었지만 검색 할 수있는 주식 목록은 업데이트되지 않았습니다. 누군가 내 코드를보고 도와 줄 수 있습니까? 당신은 "다음 페이지"에 갈 때

from selenium import webdriver 
from selenium.webdriver.common.by import By 
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC 
from selenium.common.exceptions import TimeoutException 
from bs4 import BeautifulSoup 
import re 
import selenium 

path_to_phantomjs=r'C:\Users\h.elahi\Desktop\chromedriver' 
driver = webdriver.Chrome(path_to_phantomjs) 
web= "https://markets.ft.com/data/indices/tearsheet/constituents?s=NYA:PSE" 
driver.get(web) 


element=driver.find_element_by_xpath("//li [@data-mod-pagination- 
num='3']").click() 


WebDriverWait(driver, 30) 

text="/data/equities/tearsheet" 

level=driver.find_element_by_xpath('//a[contains(@href, "%s")]' % text) 
print(level.text) 

안부

+0

을 통해 이동하는 데 사용합니다? –

답변

0

데이터를로드하는 가장 쉬운 방법은 AJAX-요청에서 직접로드하는 것입니다 :

https://markets.ft.com/data/indices/ajax/getindexconstituents?xid=566677&pagenum=2

여기 그냥 올바른 pagenum 값을 설정해야합니다. 예를 들어

:

import requests 
import json 
from lxml import etree 

for i in range(1, 2): 
    resp = requests.get('https://markets.ft.com/data/indices/ajax/getindexconstituents?xid=566677&pagenum=%s' % i) 
    // print(resp.text) 
    j = json.loads(resp.text) 
    h = etree.HTML(j['html']) 
    print(h.xpath('//a')) 

출력 :

[<Element a at 0x1024692c8>, <Element a at 0x10239cac8>, <Element a at 0x10237eac8>, <Element a at 0x10239aa08>, <Element a at 0x1024667c8>, <Element a at 0x102466c48>, <Element a a 
t 0x102466dc8>, <Element a at 0x102418f08>, <Element a at 0x102418cc8>, <Element a at 0x1024184c8>, <Element a at 0x1023a2bc8>, <Element a at 0x1023a2e88>, <Element a at 0x1023a2fc8 
>, <Element a at 0x1023a2dc8>, <Element a at 0x1023a2108>, <Element a at 0x1023a28c8>, <Element a at 0x1023a2088>, <Element a at 0x1023a2a48>, <Element a at 0x1023a23c8>, <Element a 
at 0x1023a2788>] 

이 조각 추출물 및 인쇄 또한 흥미로운 데이터를 추출 할 수 있습니다 XPath를 사용하여 응답에있는 모든 링크되어 있습니다. 다음 페이지 버튼을 클릭하고 대기 한 후

+0

정말 고마워요. – user3425989

0

말 : driver.get(driver.getCurrentUrl()) 루프에 대한이 사이트는 * 당신이 뭘 하려는지 * 방지하기 위해 작성되었음을 발생할습니까 페이지

+0

와우, getcurrenturl은 정확히 무엇을 찾고 있었습니까? – user3425989

+0

GetCurrentUrl()은 브라우저에서 URL을 반환하고, driver.get은 소스 코드를 변수로 구문 분석하여 변수를 사용하여 구문 분석합니다 웹 또는 웹 자체를 사용하십시오 ... 왜 메모리 낭비 –

+0

희망이 도움이 –