の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
使用 '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 –