PythonでBlogをPDF化して保存しようとしています。
年齢認証があるページなのですが、そのページを回避する手続きが良く分かりません。
requests.session()を使って、クッションページ(年齢認証ページ)のはいボタンに相当するURLを掴む方法があるようなのですが、
肝心のはいボタンのURLにどのアドレスを与えれば良いのかが分からないです。
Blogのソースでの該当部分は以下のようになっています。
<body class="age_verify_page"> <div class="l_age_verify_header"> <div class="l_age_verify_header_inner"> <h1 class="service_logo"> <a href="//blog.2nt.com/"> <img src="//static.2nt.com/image/blog_admin/pc/2nt_logo.png" height="26" alt="2NT ブログ"> <span>ブログ</span> </a> </h1> </div> </div> <div class="l_age_verify_body"> <div class="l_age_verify_contents"> <div class="l_age_verify_contents_inner"> <div class="age_verify_blog"> <h1 class="age_verify_blog_name">DAITA 素人M妻の調教報告</h1> <p class="age_verify_blog_msg"> </p> </div> <div class="age_verify_box"> <h2 class="age_verify_box_title">年齢の確認</h2> <p class="age_verify_box_msg"> ここから先は、成人向け(アダルト)コンテンツです。<br> 成人向けの言葉や映像が含まれています。<br> 18歳未満の者が閲覧することは禁止します。 </p> <p class="age_verify_box_question"> あなたは18歳以上ですか? </p> <p class="age_verify_box_question_sup"> (自身の居住国や地域社会における規範・法律に従ってください。) </p> <div class="age_verify_box_answer"> <form action="//blog.2nt.com/" class="age_verify_box_form"> <button class="age_verify_box_answer_no" type="submit" formaction="//blog.2nt.com/"> <span class="age_verify_box_answer_no_text">いいえ</span><span class="age_verify_box_answer_no_small">(2NTブログTOPへ)</span> </button> <input type="hidden" name="age_verification" value="0"> </form> <form method="post" class="age_verify_box_form"> <button class="age_verify_box_answer_yes" type="submit" formmethod="post">はい(入場)</button> <input type="hidden" name="age_verification" value="1"> </form> </div> </div> <div class="age_verify_notes"> cookieを有効にする必要があります。 </div> </div> </div> </div> </body>
下記のソースの?????には何を与えれば良いのでしょうか?
あるいは、上手な回避方法がありますでしょうか?
よろしくお願いします。
import requests import pdfkit from bs4 import BeautifulSoup from requests.sessions import to_key_val_list # 'クッションページの「はい」のURL' url_certification = "?????" session = requests.session() session.get(url_certification) # 'スクレイピングしたいURL' vgm_url = 'http://daitawife.blog.2nt.com/blog-entry-64.html' html_text = requests.get(vgm_url).text soup = BeautifulSoup(html_text, 'html.parser') head = "<!doctype html><html lang=\"ja\"><head><meta charset=\"UTF-8\"></head>"\ "<style>\n.vertical{\n\twriting-mode:\n\tvertical-rl;font-size:0.8em;\n}\n"\ "em {\n\t color: #ff0000;\n}\n"\ "strong {\n\t color: #0000ff;\n\t font-size:1.6em; \n\t background-color:#ffff66;\n\t}\n"\ "</style>\n<div class=\"vertical\">\n" tail = "</div>\n\t</body>\n\t\t</html>" htm=head+html_text+tail print(htm) f=open("output.htm","wb") f.write(htm.encode("UTF-8")) f.close() options = { "page-size" : "A4", "margin-top" : "0.75in", "margin-right" : "0.75in", "margin-bottom" : "0.75in", "margin-left" : "0.75in", "encoding" : "UTF-8" } pdfkit.from_string(htm, "test.pdf",options=options)