2016-11-11 9 views
0

私は緯度と経度を引き出すのを試みています。情報が目立つ、ウェブページ上に表示されていないが、私はここにHTMLでそれを見つけた:PythonとSeleniumでJavascript Textを掻き立てる

Latitude and Longitude w/in Javascript

私はすべての情報引くためにこのコードを使用しようとしています:

#import libraries 
import requests 
from bs4 import BeautifulSoup 
from selenium import webdriver 
from selenium.common.exceptions import NoSuchElementException 
from selenium.webdriver.common.keys import Keys 

for i in range(0, 30, 30): 
    #need this here for when you want more than 30 
    while i <= range: 
     i = str(i) 
     #url format offsets the restaurants in increments of 30 after the oa 
     url1 = 'https://www.tripadvisor.com/Restaurants-g294217-oa' + i + '-Hong_Kong.html#EATERY_LIST_CONTENTS' 
     r1 = requests.get(url1) 
     data1 = r1.text 
     soup1 = BeautifulSoup(data1, "html.parser") 
     for link in soup1.findAll('a', {'property_title'}): 
      #print 'https://www.tripadvisor.com/Restaurant_Review-g294217-' + link.get('href') 
      restaurant_url = 'https://www.tripadvisor.com/Restaurant_Review-g294217-' + link.get('href') 
      browser = webdriver.Chrome('C:\Python27\Chromedriver\chromedriver.exe') 
      # use xpath to get to the information in the JS 
      print browser.find_element_by_xpath("""/html/body/script[22]""") 

をコードを実行すると、要素が見つからないことがわかります。たぶん私はちょっと脳死しているかもしれませんが、新鮮な眼がこれを見て、私がこれを間違っているかどうかを知らせてくれるか、これについて別の方法があると私はすべての耳です。

+0

「範囲<=範囲:」の間は問題がありませんが、「範囲」は関数です。 – Brian

+0

ありがとうございます。私が投稿した画像のリンクを見ると。私はその写真でその情報を引き出そうとしています。しかし、私はそれを実行すると、そのコードは、xpathの要素を見つけることができないことを私に伝えます。 – dtrinh

答えて

0

あなたはセレンとしてselenium webdriverを使用しているときrequestsBeautifulSoupパッケージを使用することのない点は、Webページ(requests)を開き、独自にコンテンツ(BeautifulSoup)を取得することができますがありません。以下は、あなたがセレンを使って明白に達成しようとしているものの大まかな構造です。

from selenium import webdriver 
from selenium.common.exceptions import NoSuchElementException 
from selenium.webdriver.common.keys import Keys 


browser = webdriver.Chrome('C:\Python27\Chromedriver\chromedriver.exe') 
for counter in range(0, 30, 30): 
    #need this here for when you want more than 30 
    while i <= counter: 
     i = str(i) 
     url1 = 'https://www.tripadvisor.com/Restaurants-g294217-oa' + i + '-Hong_Kong.html#EATERY_LIST_CONTENTS' 
     browser.get(url1) # this will redirect to webpage 
     # use xpath to get to the information in the JS 
     print browser.find_element_by_xpath("""/html/body/script[22]""") 
関連する問題