كيف يمكنني وضع جميع أوامر Twill معًا في جزء واحد من التعليمات البرمجية في ملف .py؟
سؤال
لقد بدأت للتو في الاستكشاف نسيج قطني طويل.
Twill هي لغة برمجة رائعة لتصفح الويب وهي تفعل كل ما أريد !!!
حتى الآن كنت أستخدم Twill من قذيفة Python (الخمول (Python GUI) لأكون دقيقًا) وأفعل أشياء هناك في طريق تنفيذ الأوامر واحدة تلو الأخرى (أقصد ، أكتب أمرًا واحدًا ، ثم اكتب الأمر التالي):
(مصدر: narod.ru)
(مصدر: narod.ru)
(مصدر: narod.ru)
(مصدر: narod.ru)
(مصدر: narod.ru)
(مصدر: narod.ru)
(مصدر: narod.ru)
لكنني لا أعرف كيفية وضع كل هذه الأوامر معًا في ملف .py واحد ، بحيث يتم تنفيذها جميعًا تلو الأخرى تلقائيًا.
يبدو أن هناك مثل هذا الاحتمال في تلي. هذا المثال من صفحة توثيق Twill (يمكنك رؤيته هنا) يوضح لنا قطعة واحدة من الكود تتكون من عدة أوامر:
(مصدر: narod.ru)
لذا ، سؤالي هو: كيف يمكنني وضع جميع الأوامر معًا في تليك؟
تحديث 1:
(هذا التحديث هو ردي على S.Mark)
مرحبا ، S.Mark !!! أنا آسف على الرد المتأخر. بادئ ذي بدء ، بعض المعلومات حول موقع المجلدات المتعلقة ببيثون وبيثون:
المسار الذي يتم فيه تثبيت Python2.5 على جهاز الكمبيوتر الخاص بي: C: Python25
المسار إلى Twill-0.9 على جهاز الكمبيوتر الخاص بي الآن: E: tmp Twill-0.9
لنفترض أنني أريد تنفيذ الأوامر التالية تلقائيًا:
go http://www.yahoo.com
save_html result.html
يجب أن يبحث هذا الرمز في صفحة Yahoo ثم حفظ رمز HTML الخاص به في ملف result.html. لذا ، في محاولة لاتباع التعليمات الخاصة بك ، قمت أولاً بإنشاء ملف "test.txt" الذي يحتوي على هذا الرمز يتكون من سطرين فقط وحفظ هذا الملف كـ "test.twill" في المجلد Twill-0.9 ، مما يعني أن المسار الكامل لذلك كان الملف الآن e: tmp twill-0.9 test.twill
ثم حاولت تمرير اسم الملف كمعلمة إلى أمر twill-sh بعدة طرق مختلفة ، لكن لم ينجح أبدًا (يجب أن أفعل شيئًا خاطئًا):
(مصدر: narod.ru)
(مصدر: narod.ru)
لكنك تعرف ماذا ، قررت تجربة بعض الشيء وأنشأت ملف test.py الذي يحتوي أيضًا على هذين الأمرين. هذا الملف الذي وضعته أيضًا في المجلد Twill-0.9 (E: tmp Twill-0.9 test.py) ثم قررت أن أحاول تشغيله من Twill Shell باستخدام Twill's Runfile Command ، وبشكل مفاجئ ، لقد نجحت! :
(مصدر: narod.ru)
بعد تشغيله ، بحثت عن مجلد C: Python25 ووجدت ملف النتيجة.
حسنًا ، ما فعلته هنا هو مجرد تشغيل ملف من Twill Shell باستخدام أمر Twill. على الرغم من أنه في الوقت الحالي هو ما أحتاجه بالضبط ، فإن المؤيدين الآخرين (كما ترون أدناه) يشيرون إلى أنني يجب أن أفعل كل الأشياء من Python Shell ، وليس من Twill Shell ، وهذا شيء ما زلت لا أعرف كيف أفعل.
ستكون خطوتي التالية هي محاولة تشغيل رمز مماثل على "Google App Engine" ، ولكن هناك ، على حد علمي ، يتم التعرف على Python فقط ، وليس Twill ، مما يعني أنه إذا كنت أعرف فقط كيفية القيام بالأشياء في Twill ، ولكن ليس في Python ، لن أتمكن من تنفيذ "Google App Engine".
تحديث 2:
(الجمعة 23 ، أبريل 2010 ، 3:48:15 صباحًا (GMT+0.00))
(هذا التحديث هو ردي الثاني على S.Mark)
يبدو أن تشغيله من موجه الأوامر لم ينجح أيضًا:
(مصدر: narod.ru)
المحلول
ضع أوامر Twill الخاصة بك في ملف ، على سبيل المثال test.twill
setlocal query "twill Python"
go http://google.com/
fv 1 q $query
submit btnI # use the "I'm feeling lucky" button
show
ثم فقط تمرير اسم الملف كمعلمة إلى أمر twill-sh ، مثل
python twill-sh test.twill
وقد ترغب في التحقق من رموز عينة .Twill tests
مجلد مصدر تلي
test-back.twill
test-basic.twill
test-dns.twill
test-equiv-refresh.twill
test-find.twill
test-form.twill
test-formfill.twill
test-global-form.twill
test-go-exit.twill
....
نصائح أخرى
هنا في العمل (تم تغييره قليلاً):
>>> import twill.commands
>>> import BeautifulSoup
>>>
>>> class browser:
... def __init__(self, url="http://www.google.com",log = None):
... self.a=twill.commands
... self.a.config("readonly_controls_writeable", 1)
... self.b = self.a.get_browser()
... self.b.set_agent_string("Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14")
... self.log = log
... self.b.clear_cookies()
... self.url=url
... def googleQuery(self, query="python code"):
... self.b.go(self.url)
... #self.b.showforms()
... f = self.b.get_form("f")
... #print "form is %s" % f
... f["q"] = query
... self.b.clicked(f, "btnG")
... self.b.submit()
... pageContent = self.b.get_html()
... soup=BeautifulSoup.BeautifulSoup(pageContent)
... ths = soup.findAll(attrs={"class" : "l"})
... for a in ths:
... print a
...
>>> t=browser()
>>> t.googleQuery("twill queries")
==> at http://www.google.ie/
Note: submit is using submit button: name="btnG", value="Google Search"
<a href="http://pyparsing.wikispaces.com/WhosUsingPyparsing" class="l" onmousedown="return clk(this.href,'','','res','1','','0CBMQFjAA')">pyparsing - WhosUsingPyparsing</a>
<a href="http://www.mail-archive.com/twill@lists.idyll.org/msg00048.html" class="l" onmousedown="return clk(this.href,'','','res','2','','0CBcQFjAB')">Re: [<em>twill</em>] <em>query</em>: docs, and web site.</a>
<a href="http://www.mail-archive.com/twill@lists.idyll.org/msg00050.html" class="l" onmousedown="return clk(this.href,'','','res','3','','0CBkQFjAC')">Re: [<em>twill</em>] <em>query</em>: docs, and web site.</a>
<a href="http://www.genealogytoday.com/surname/finder.mv?Surname=Twill" class="l" onmousedown="return clk(this.href,'','','res','4','','0CB4QFjAD')"><em>Twill</em> Genealogy and Family Tree Resources - Surname Finder</a>
<a href="http://a706cheap-apparel.hobby-site.com/ladies-cotton-faded-twill-le-chameau-breeks-42" class="l" onmousedown="return clk(this.href,'','','res','5','','0CCEQFjAE')">Ladies Cotton Faded <em>Twill</em> Le Chameau Breeks 42</a>
<a href="http://twill.idyll.org/examples.html" class="l" onmousedown="return clk(this.href,'','','res','6','','0CCMQFjAF')"><em>twill</em> Examples</a>
<a href="http://panjiva.com/Sri-Lankan-Manufacturers-Of/twill+capri" class="l" onmousedown="return clk(this.href,'','','res','7','','0CCcQFjAG')">Sri-Lankan <em>Twill</em> Capri Manufacturers | Sri-Lankan Suppliers of <b>...</b></a>
<a href="http://c586cheap-apparel.dyndns.ws/twill-beige-blazer" class="l" onmousedown="return clk(this.href,'','','res','8','','0CCoQFjAH')"><em>Twill</em> beige blazer</a>
<a href="http://stackoverflow.com/questions/2267537/how-do-you-use-relative-paths-for-twill-tests" class="l" onmousedown="return clk(this.href,'','','res','9','','0CCwQFjAI')">How do you use Relative Paths for <em>Twill</em> tests? - Stack Overflow</a>
<a href="http://mytextilenotes.blogspot.com/2010/01/introduction-to-twill-weave.html" class="l" onmousedown="return clk(this.href,'','','res','10','','0CC8QFjAJ')">My Textile Notes: Introduction to <em>Twill</em> Weave</a>
>>>
أستخدم Ubuntu لذا أستخدم ما يلي لتثبيت BeautifulSoup و Twill:
sudo apt-get install BeautifulSoup*
sudo apt-get install python-twill*
كيف يساعد هذا
أ
أعتقد أنه بدلاً من استخدام قذيفة Twill ، يجب عليك بدلاً من ذلك استدعاء الوظائف مباشرة باستخدام API Twill Python http://twill.idyll.org/python-api.html.
import string, re, sys, os
import twill.commands
class browser:
def __init__(self, url="www.google.com", query="python code", log = None):
self.a=twill.commands
self.a.config("readonly_controls_writeable", 1)
self.b = self.a.get_browser()
self.b.set_agent_string("Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14")
self.log = log
self.b.clear_cookies()
self.url=url
self.b.go(self.url)
f = self.b.get_form("1")
# self.log.debug("form is %s" % f)
f["q"] = query
self.b.submit()
self.log.debug( "Links\n%s" % self.b.showlinks())
self.log.debug( "Forms\n%s" % self.b.showforms())
pageContent = self.b.get_html()
self.log.debug("html is <<%s>>" % pageContent)