我是noob的定義。我對python幾乎一無所知,並且正在尋求幫助。我可以閱讀足夠的代碼來改變變量以適應我的需求,但是當我做一些原始代碼不需要的東西...我迷路了。Python - 使腳本循環,直到條件滿足,併爲每個循環使用不同的代理地址
所以這裏是交易,我找到了一個craigslist(CL)標記腳本,最初搜索所有CL網站和標記的帖子,其中包含一個特定的關鍵字(它被寫爲標記所有提到scienceology的帖子)。
我改變它只在我的一般區域(15個網站而不是437)搜索CL網站,但它仍然會查找已更改的特定關鍵字。我想自動標記持續垃圾郵件的人,並且很難排序,因爲我在CL上做了很多業務,從郵件中排序。
我想讓腳本執行循環,直到它不能再在每個循環之後找到滿足標準更改代理服務器的帖子。並在劇本里面放置代理/ IP地址的地方
我期待着您的回覆。
這裏是改變的代碼,我有:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib
from twill.commands import * # gives us go()
areas = ['sfbay', 'chico', 'fresno', 'goldcountry', 'humboldt', 'mendocino', 'modesto', 'monterey', 'redding', 'reno', 'sacramento', 'siskiyou', 'stockton', 'yubasutter', 'reno']
def expunge(url, area):
page = urllib.urlopen(url).read() # <-- and v and vv gets you urls of ind. postings
page = page[page.index('<hr>'):].split('\n')[0]
page = [i[:i.index('">')] for i in page.split('href="')[1:-1] if '<font size="-1">' in i]
for u in page:
num = u[u.rfind('/')+1:u.index('.html')] # the number of the posting (like 34235235252)
spam = 'https://post.craigslist.org/flag?flagCode=15&postingID='+num # url for flagging as spam
go(spam) # flag it
print 'Checking ' + str(len(areas)) + ' areas...'
for area in ['http://' + a + '.craigslist.org/' for a in areas]:
ujam = area + 'search/?query=james+"916+821+0590"+&catAbb=hhh'
udre = area + 'search/?query="DRE+%23+01902542+"&catAbb=hhh'
try:
jam = urllib.urlopen(ujam).read()
dre = urllib.urlopen(udre).read()
except:
print 'tl;dr error for ' + area
if 'Found: ' in jam:
print 'Found results for "James 916 821 0590" in ' + area
expunge(ujam, area)
print 'All "James 916 821 0590" listings marked as spam for area'
if 'Found: ' in dre:
print 'Found results for "DRE # 01902542" in ' + area
expunge(udre, area)
print 'All "DRE # 01902542" listings marked as spam for area'
如果你只使用'go',只導入'go':'從twill.commands導入go' – askewchan 2013-02-19 21:17:19
導入錯誤:沒有名爲模塊去 – 2013-02-19 22:18:10
奇怪:HTTP://斜紋.idyll.org/python-api.html說:'從twill.commands進口去' – askewchan 2013-02-19 22:24:07