<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Requests - イチゾーのブログ</title>
	<atom:link href="https://ichizo.biz/tag/requests/feed" rel="self" type="application/rss+xml" />
	<link>https://ichizo.biz</link>
	<description>システム開発やWordPressについてなど</description>
	<lastBuildDate>Thu, 22 Dec 2016 03:31:47 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.1</generator>
<site xmlns="com-wordpress:feed-additions:1">120380898</site>	<item>
		<title>PythonのRequestsを使って検索結果を取得する</title>
		<link>https://ichizo.biz/2016/12/22/python-search.html</link>
					<comments>https://ichizo.biz/2016/12/22/python-search.html?noamp=mobile#respond</comments>
		
		<dc:creator><![CDATA[一蔵]]></dc:creator>
		<pubDate>Thu, 22 Dec 2016 03:31:47 +0000</pubDate>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Requests]]></category>
		<guid isPermaLink="false">http://ichizo.biz/?p=78</guid>

					<description><![CDATA[<p>検索結果一覧を自動取得したい 検索サービスを提供しているWE [&#8230;]</p>
<p>The post <a href="https://ichizo.biz/2016/12/22/python-search.html">PythonのRequestsを使って検索結果を取得する</a> first appeared on <a href="https://ichizo.biz">イチゾーのブログ</a>.</p>]]></description>
										<content:encoded><![CDATA[<h2>検索結果一覧を自動取得したい</h2>
<p>検索サービスを提供しているWEBページに対して、検索を実行しその結果ページの内容を取得したいということで、<br />
Pythonを使って検索要求を投げ、その結果ページをスクレイピングするプログラムを実装したいと思います。</p>
<p>環境は「<a href="http://ichizo.biz/2016/12/13/anaconda.html">AnacondaでWindows上にPython環境を構築</a>」で構築したWindows上のものを使用します。</p>
<p>PythonのRequestsモジュールを使用します。</p>
<p>RequestsはすでにAnacondaに含まれているようなので、これを使用します。</p>
<p>今回は、Requestsを使って検索リクエストを送信します。</p>
<p>サンプルとして、「python」をYahoo!検索した結果を取得するプログラムを実装してみます。</p>
<pre class="lang:python decode:true " title="searchy.py" >
# -*- coding: utf-8 -*-
"""
Created on Thu Dec 22 11:14:21 2016

@author: arina
"""

import requests

url = 'http://search.yahoo.co.jp/search'
params = {'p':'python',
          'search.x':'1',
          'fr':'top_ga1_sa',
          'tid':'top_ga1_sa',
          'ei':'UTF-8',
          'aq':'',
          'oq':'',
          'afs':'',}

def send_request():
    response = requests.get(url, params)
    print (response.text)


if __name__ == '__main__':
    send_request()
</pre>
<p>これだけでできてしまいました。</p>
<p>URLやパラメーターに使用している値は、Yahoo!のソースを見て取得しました。</p>
<p>検索を行うformタグのURLが「http://search.yahoo.co.jp/search」になっていて、<br />
キーワードを入力するボックスが「p」になっていたので、<br />
paramsに「&#8217;p&#8217;:&#8217;python&#8217;」を指定します。<br />
paramsのほかの値は、form内でhiddenで設定されていたものを入れました。</p>
<p>検索リクエストを送信するのは、「response = requests.get(url, params)」の1行だけです。<br />
Yahoo!ページ内のformタグのmethodが「get」になっていたので、<br />
「requests.get」を使用します。<br />
引数にurlとparamsを指定するだけです。</p>
<p>検索結果はresponse内に入っているので、テキストとして取り出すには<br />
「response.text」とすればOKです。</p>
<p>びっくりするほど簡単でした。</p><p>The post <a href="https://ichizo.biz/2016/12/22/python-search.html">PythonのRequestsを使って検索結果を取得する</a> first appeared on <a href="https://ichizo.biz">イチゾーのブログ</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://ichizo.biz/2016/12/22/python-search.html/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">78</post-id>	</item>
	</channel>
</rss>
