Skip to main content
 首页 » 编程设计

Python之使脚本循环直到满足条件并为每个循环使用不同的代理地址

2025年05月04日13mayingbao

我是菜鸟的定义。我对 python 几乎一无所知,正在寻求帮助。我可以阅读足够多的代码来改变变量以满足我的需要/需要,但是当涉及到做一些原始代码没有要求的事情时……我迷路了。

事情是这样的,我找到了一个 craigslist(CL) 标记脚本,它最初搜索所有 CL 站点并标记包含特定关键字的帖子(它被编写来标记所有提到山达基的帖子)。

我将其更改为仅搜索我的一般区域中的 CL 站点(15 个站点而不是 437 个站点),但它仍然会查找已更改的特定关键字。我想自动标记那些不断向 CL 发送垃圾邮件并使其难以分类的人,因为我在 CL 上做了很多业务,从分类到帖子。

我想要脚本做的是循环,直到它不再能找到满足每次循环后更改代理服务器的条件的帖子。在脚本中的一个地方,我将在其中放置代理/s 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&amppostingID='+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' 

请您参考如下方法:

你可以像这样创建一个常量循环:

while True: 
    if condition : 
        break 

Itertools 有一些迭代 http://docs.python.org/2/library/itertools.html 的技巧

值得注意的是,查看 itertools.cycle

(这些是指向正确方向的指针。您可以用一个、另一个甚至两个来制定解决方案)