Python 3 RoboBrowser ตัวแทนใหม่ของ Mechanize

อยากจะเขียนคำขึ้นต้นสวย ๆ แต่ว่าไม่อยากอารัมพบทเยอะ ดังนั้นเริ่มเลยละกัน
เนื่องจาก Lib Mechanize นั้นใช้ไม่ได้ใน Python 3 เนื่องจากผู้พัฒนาไม่ทำต่อนั่นเอง ทำให้ผมซึ่งมีความต้องการที่จะเขียนสคริปต์เพื่อศึกษาการเขียน Web Exploit เลยต้องมาควานหา Module อื่น ๆ ที่ใช้แทน Mechanize ได้ซึ่งจริง ๆ แล้วก็ต้องเรียกว่ามีหลายเหตุผลที่ทำให้อยากกลับมาเขียน Python และอยากศึกษามันไม่ลึกขึ้นไปอีกนั่นเอง เพราะมันตอบโจทย์หลาย ๆ อย่างที่ผมต้องการเช่น ถ้าหากว่ามีไอเดียอยากเป็น Maker ซึ่ง Python ก็สามารถเขียนโปรแกรม Control ในส่วนของ Hardware ได้ ถ้าหากว่าอยากโพสขายของบนเว็บก็เขียน Bot ปั้มกระทู้ขึ้นมาได้ และถ้าอยากศึกษาช่องโหว่ Python ก็เป็นอีกภาษาที่เหมาะแก่การนำมาพัฒนา Exploit นั่นเอง


ดังนั้นไม่แพล่มละ เริ่มกันเลยดีกว่า หลังจากที่ค้นหาข้อมูลอยู่นานมีหลาย ๆ คนแนะนำให้ใช้ Lib Requests กับ BeatifulSoup 4 เหมือนกันแต่พยายามใช้ Requests หลายครั้งแล้วเขียนไม่สำเร็จ เรียกว่า "กาก" ก็ได้ครับยอมรับ เลยไปค้นหาจาก Google และได้ค้นพบคำตอบจาก StackOverflow ว่ามี Lib ทดแทนตัวนึงชื่อว่า RoboBrowser นั่นเองจึงได้เข้าไปนั่งอ่านมา ผลปรากฏแว่ ก็สามารถใช้งานทดแทนกันได้ โดยผมถือว่าหลาย ๆ ฟังก์ชันเจ๋งกว่า Mechanize อีก
หรือว่าผมไม่เคยเขียน Mechanize ไปถึงระดับนั้นนะอันนี้ก็ไม่รู้เช่นกันครับแต่ก็ต้องลองกันแหละครับทีนี้

เรามาเริ่มกันเลยดีกว่า


ทำการติดตั้ง Module RoboBrowser โดยกดปุ่ม Start พิมพ์ "cmd" แล้วกด Enter แล้วพิมพ์คำสั่งด้านล่างลงไป
Install RoboBrowser
1
pip install robobrowser
รอจนระบบดำเนินการติดตั้งจนแล้วเสร็จ จากนั้นให้เปิดโปรแกรม Editor ที่คุณใช้ขึ้นมา แล้วพิมพ์ code ตามนี้ลงไป
First Code with RoboBrowser
1
2
3
4
5
6
#!/usr/bin/python 3
from robobrowser import RoboBrowser
 
br = RoboBrowser(history=True, parser="html.parser", user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36")
print(br.response.text)

แล้วดูผลลัพธ์
Output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<link href="https://ajax.cloudflare.com" rel="preconnect"><link href="https://fonts.gstatic.com" rel="preconnect"><link href="https://1.bp.blogspot.com" rel="preconnect"><link href="https://pagead2.googlesyndication.com" rel="preconnect"><link href="https://cdnjs.cloudflare.com" rel="preconnect"><link href="https://use.fontawesome.com" rel="preconnect"><link href="https://adservice.google.com" rel="preconnect"><link href="https://googleads.g.doubleclick.net" rel="preconnect"><link href="https://connect.facebook.net" rel="preconnect"><link href="https://www.google-analytics.com" rel="preconnect"><link href="https://ajax.cloudflare.com" rel="dns-prefetch"><link href="https://fonts.gstatic.com" rel="dns-prefetch"><link href="https://1.bp.blogspot.com" rel="dns-prefetch"><link href="https://pagead2.googlesyndication.com" rel="dns-prefetch"><link href="https://cdnjs.cloudflare.com" rel="dns-prefetch"><link href="https://use.fontawesome.com" rel="dns-prefetch"><link href="https://adservice.google.com" rel="dns-prefetch"><link href="https://googleads.g.doubleclick.net" rel="dns-prefetch"><link href="https://www.google-analytics.com" rel="dns-prefetch"><link href="https://connect.facebook.net" rel="dns-prefetch">
<!--Homepage-->
<title>The Hacker News — Most Popular Cyber Security, Hacking News Site</title>
 
 
 
 
 
<style type="text/css">
<!-- /*<style id='page-skin-1' type='text/css'><!--
*/
--></style>
 
<!--<head>
 
</head>-->
 
<header class="header clear">
<div class="top-bar cf">
<div class="top-bar-box cf">
<span class="top-follow"><a href="https://www.blogger.com/blogger.g?blogID=4431628076998358629#follow-us">Follow us</a>
<a href="https://www.facebook.com/thehackernews" rel="nofollow noopener" target="_blank"><i aria-hidden="true" class="fa fa-facebook"></i></a>
<a href="https://twitter.com/thehackersnews" rel="nofollow noopener" target="_blank"><i aria-hidden="true" class="fa fa-twitter"></i></a>
<a href="https://www.linkedin.com/company/the-hacker-news/" rel="nofollow noopener" target="_blank"><i aria-hidden="true" class="fa fa-linkedin"></i></a>
<a href="https://www.youtube.com/user/hackernewsvideos?sub_confirmation=1" rel="nofollow noopener" target="_blank"><i aria-hidden="true" class="fa fa-youtube"></i></a><a href="https://feeds.feedburner.com/TheHackersNews" rel="nofollow noopener" target="_blank"><i aria-hidden="true" class="fa fa-rss"></i></a></span>
</div>
</div>
</header>
<nav class="menu cf" itemscope="" itemtype="http://schema.org/SiteNavigationElement" role="navigation">
<div class="m-menu-box cf">
<ul class="cf m-menu-ul">
<li><a href="https://www.blogger.com/"><i aria-hidden="true" class="fa fa-home"></i> Home</a></li>
<li><a href="https://www.blogger.com/blogger.g?blogID=4431628076998358629#email-outer"><i aria-hidden="true" class="fa fa-envelope"></i> Subscribe</a></li>
<li><a href="https://deals.thehackernews.com/" rel="noopener" target="_blank"><i aria-hidden="true" class="fa fa-shopping-cart"></i> Deals</a></li>
</ul>
</div>
<----- code="" pre="">
จะเห็นได้ว่าเขียน Code ง่าย ๆ ไม่กี่บรรทัดก็สามารถเปิดหน้าเว็บได้แล้ว
***ไว้ว่างจะมาอัพเดทบทความนี้ให้สมบูรณ์ยิ่งขึ้นสำหรับตอนนี้หลับฝันดี ราตรีสวัสดิ์ :3
 
 
หากสนใจหาความรู้เพิ่มเติมคลิก<a href="https://github.com/jmcarp/robobrowser">ที่นี่</a>
 
 
 
 
<!---------></nav>

No comments:

Post a Comment

วิธีการติดตั้ง และตั้งค่า Apache2 พร้อมเปิดใช้งาน HTTPS ด้วยวิธี Self-Sign บน Ubuntu 18.04.1 LTS

ดีจร้า วันนี้ช่วงเช้าว่างเลยมาเขียนบทความเพิ่มเติมเกี่ยวกับการติดตั้ง และตั้งค่า Apache2 ให้สามารถใช้งานผ่าน HTTPS ได้กันมาเริ่มกันเลยครับ ...