私はGoogleの(連続した結果ページの、すなわち束、注目コンテンツの表と各)に類似した構造を持つサイトのパーサを構築するに探しています。
Seleniumパッケージ(ページ要素ベースのサイトナビゲーション用)とBeautifulSoup(html解析用)の組み合わせは、書かれたコンテンツを収穫するための選択肢のようです。あなたはそれが役に立つかもしれませんが、スクラップを抑止するためにGoogleがどんな種類の防御を行っているかはわかりません。
セレン、beautifulsoupとgeckodriverを使用してMozilla Firefox用可能な実装:この後
from bs4 import BeautifulSoup, SoupStrainer
from bs4.diagnose import diagnose
from os.path import isfile
from time import sleep
import codecs
from selenium import webdriver
def first_page(link):
"""Takes a link, and scrapes the desired tags from the html code"""
driver = webdriver.Firefox(executable_path = 'C://example/geckodriver.exe')#Specify the appropriate driver for your browser here
counter=1
driver.get(link)
html = driver.page_source
filter_html_table(html)
counter +=1
return driver, counter
def nth_page(driver, counter, max_iter):
"""Takes a driver instance, a counter to keep track of iterations, and max_iter for maximum number of iterations. Looks for a page element matching the current iteration (how you need to program this depends on the html structure of the page you want to scrape), navigates there, and calls mine_page to scrape."""
while counter <= max_iter:
pageLink = driver.find_element_by_link_text(str(counter)) #For other strategies to retrieve elements from a page, see the selenium documentation
pageLink.click()
scrape_page(driver)
counter+=1
else:
print("Done scraping")
return
def scrape_page(driver):
"""Takes a driver instance, extracts html from the current page, and calls function to extract tags from html of total page"""
html = driver.page_source #Get html from page
filter_html_table(html) #Call function to extract desired html tags
return
def filter_html_table(html):
"""Takes a full page of html, filters the desired tags using beautifulsoup, calls function to write to file"""
only_td_tags = SoupStrainer("td")#Specify which tags to keep
filtered = BeautifulSoup(html, "lxml", parse_only=only_td_tags).prettify() #Specify how to represent content
write_to_file(filtered) #Function call to store extracted tags in a local file.
return
def write_to_file(output):
"""Takes the scraped tags, opens a new file if the file does not exist, or appends to existing file, and writes extracted tags to file."""
fpath = "<path to your output file>"
if isfile(fpath):
f = codecs.open(fpath, 'a') #using 'codecs' to avoid problems with utf-8 characters in ASCII format.
f.write(output)
f.close()
else:
f = codecs.open(fpath, 'w') #using 'codecs' to avoid problems with utf-8 characters in ASCII format.
f.write(output)
f.close()
return
、それが呼び出すだけの問題である:このスクリプトは、その結果を前提としていること
link = <link to site to scrape>
driver, n_iter = first_page(link)
nth_page(driver, n_iter, 1000) # the 1000 lets us scrape 1000 of the result pages
注意スクラップしようとしているページには順番に番号が付けられ、それらの番号は 'find_element_by_link_text'を使用してスクラップページのhtmlから取り出すことができます。ページから要素を取得する他の方法については、セレン資料hereを参照してください。
また、ブラウザと通信するためには、これが依存するパッケージとセレンが必要なドライバをダウンロードする必要があります(この場合はgeckodriver、geckodriverをダウンロードしてフォルダに入れてから'executable_path'の実行可能ファイルに)
これらのパッケージを使用すると、時間パッケージ(Pythonのネイティブ)を使用してサーバー要求を広め、あなたが掻爬しているサーバー。私は自分自身のプロジェクトのためにそれを必要としませんでしたが、最初の質問に対するhereの2番目の答えを見ると、4番目のコードブロックで使用された時間モジュールの実装例です。
Yeeeeaaaahhh ...上位の担当者が編集してbeautifulsoup、seleniumおよびtimeのドキュメントへのリンクを追加できれば、それは素晴らしいことです。