2016-12-26 5 views
2

のWindows 10のホーム Pythonは2.7(も3.3で試してみました)64ビット Pycharmコミュニティ2006年3月1日使用Pythonは与えられた検索語句とURL

のPythonに非常に新しいので、私と一緒にクマのためのGoogleの検索結果を通過します。

Googleに行くスクリプトを書いて検索フレーズを入力し、検索ボタンをクリックし、URL(または任意の文字列)の検索結果を調べ、そのページに結果がない場合は、 [次へ]ボタンをクリックし、次のページを繰り返して、URLを見つけて停止し、結果が見つかったページを印刷します。

ちょうどバックグラウンドで実行され、私に結果を与えるかどうかは正直に気にしません。最初、私はそれをlitterallyブラウザを開いて、Xpath経由でブラウザオブジェクト(検索フィールドと検索ボタン)を見つけて実行しようとしていました。

インストールして試したモジュールが表示されます。そして、私がStackOverflowで見つけたほぼすべてのコード例を2日間試してみました。

誰かが私に最高と他の方向を動作させるモジュールを教えてくれれば非常に感謝しています!

私が試した特定のモジュールは、Selenim、Clipboard、MechanicalSoup、BeautifulSoup、webbrowser、urllib、enter image description here unittestとPopenでした。

ありがとうございます! Chantz

import clipboard 
import json as m_json 
import mechanicalsoup 
import random 
import sys 
import os 
import mechanize 
import re 
import selenium 
from selenium import webdriver 
from selenium.webdriver.common.by import By 
import time 
import unittest 
import webbrowser 
from mechanize import Browser 
from bs4 import BeautifulSoup 
from subprocess import Popen 
###################################################### 
###################################################### 
# Xpath Google Search Box 
# //*[@id="lst-ib"] 
# Xpath Google Search Button 
# //*[@id="tsf"]/div[2]/div[3]/center/input[1] 
###################################################### 
###################################################### 
webbrowser.open('http://www.google.com') 
time.sleep(3) 

clipboard.copy("abc") # now the clipboard content will be string "abc" 
driver = webdriver.Firefox() 
driver.get('http://www.google.com/') 
driver.find_element_by_id('//*[@id="lst-ib"]') 

text = clipboard.paste("abc") # text will have the content of clipboard 
print('text') 

# browser = mechanize.Browser() 
# url = raw_input("http://www.google.com") 
# username = driver.find_element_by_xpath("//form[input/@name='username']") 
# username = driver.find_element_by_xpath("//form[@id='loginForm']/input[1]") 
# username = driver.find_element_by_xpath("//*[@id="lst-ib"]") 
# elements = driver.find_elements_by_xpath("//*[@id="lst-ib"]") 
# username = driver.find_element_by_xpath("//input[@name='username']") 

# CLICK BUTTON ON PAGE 
# http://stackoverflow.com/questions/27869225/python-clicking-a-button-on-a-webpage 
+0

使用 'requests'と' BeautifulSoup'は、 '追加ヘッダー= { 'のUser-Agent': 'のMozilla/5.0(X11;のUbuntu、Linuxのx86_64で、RV:46.0​​)のGecko/20100101 Firefoxの/ 46.0' '受け入れ言語: 'en-US、ja; q = 0.5' } 'を要求し、ブロックされないようにすべての要求の間に数秒間スリープします。ボタンなどをクリックする必要はなく、URLは検索クエリとページを定義します(例: http://google.com/search?q=stuff&start=10 –

答えて

2

セレンは、実際にこのスクリプトを使用するのは簡単/良いモジュールになります。この場合は他に何も必要ありません。

from selenium import webdriver 
import time 
driver = webdriver.Firefox() 
url = 'https://www.google.nl/' 
linkList = [] 
driver.get(url) 


string ='search phrase' 
text = driver.find_element_by_xpath('//*[@id="lst-ib"]') 
text.send_keys(string) 
time.sleep(2) 
linkBox = driver.find_element_by_xpath('//*[@id="nav"]/tbody/tr') 
links = linkBox.find_elements_by_css_selector('a') 

for link in links: 
    linkList.append(link.get_attribute('href')) 

print linkList 

このコードは、ブラウザを開いて、検索語句を入力し、別のページ番号のリンクを取得します:あなたの目標を達成する最も簡単な方法は、おそらくこのようなものです。ここからは、ブラウザのすべてのリンクを入力し、検索フレーズがあるかどうかを調べるループを作成するだけです。

私はこれが役立つことを望みます。さらなるご質問がある場合はお知らせください。

+0

返事をお寄せいただきありがとうございます、遅れてご返事ありがとうございました。私は努力に戻って、もう一度ありがとう! – Cambo415

関連する問題