Python正则表达式,用于根据子字符串删除刮取结果?

2024-04-27 05:10:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我用python写了一个刮刀。我有一组字符串,我想在页面上搜索,并从结果中,我想删除这些结果,其中包含的字从另一组字符串我有。你知道吗

这是密码-

def find_jobs(self, company, soup):
        allowed = re.compile(r"Developer|Engineer|Designer|Admin|Manager|Writer|Executive|Lead|Analyst|Editor|"
                             r"Associate|Architect|Recruiter|Specialist|Scientist|Support|Expert|SSE|Head|"
                             r"Producer|Evangelist|Ninja", re.IGNORECASE)
        not_allowed = re.compile(r"^responsibilities$|^description$|^requirements$|^experience$|^empowering$|^engineering$|^"
                                 r"find$|^skills$|^recruiterbox$|^google$|^communicating$|^associated$|^internship$|^you$|^"
                                 r"proficient$|^leadsquared$|^referral$|^should$|^must$|^become$|^global$|^degree$|^good$|^"
                                 r"capabilities$|^leadership$|^services$|^expertise$|^architecture$|^hire$|^follow$|^jobs$|^"
                                 r"procedures$|^conduct$|^perk$|^missed$|^generation$|^search$|^tools$|^worldwide$|^contact$|^"
                                 r"question$|^intern$|^classes$|^trust$|^ability$|^businesses$|^join$|^industry$|^response$|^"
                                 r"using$|^work$|^based$|^grow$|^provide$|^understand$|^header$|^headline$|^masthead$|^office$", re.IGNORECASE)

        profile_list = set()
        k = soup.body.findAll(text=allowed)
        for i in k:
            if len(i) < 60 and not_allowed.search(i) is None:
                profile_list.add(i.strip().upper())
        self.update_jobs(company, profile_list)

所以我在这里面临一个问题。使用not_allowed中的锚定标记,像//HEADLINE-BGABILITY TO LEAD & MENTOR A TEAM这样的字符串可以通过,尽管我在not_allowed中有字符串headlineability。如果我删除了锚定标记,但是由于not_allowed中的字符串ability,像SCALABILITY ENGINEER这样的字符串没有被保存,那么这些都会被删除。所以作为regex的新手,我不知道如何才能让它工作。之前我用过这个-

def find_jobs(self, company, soup):
        allowed = re.compile(r"Developer|Designer|Engineer|Admin|Manager|Writer|Executive|Lead|Analyst|Editor|"
                             r"Associate|Architect|Recruiter|Specialist|Scientist|Support|Expert|SSE|Head"
                             r"Producer|Evangelist|Ninja", re.IGNORECASE)
        not_allowed = ['responsibilities', 'description', 'requirements', 'experience', 'empowering', 'engineering',
                       'find', 'skills', 'recruiterbox', 'google', 'communicating', 'associated', 'internship',
                       'proficient', 'leadsquared', 'referral', 'should', 'must', 'become', 'global', 'degree', 'good',
                       'capabilities', 'leadership', 'services', 'expertise', 'architecture', 'hire', 'follow',
                       'procedures', 'conduct', 'perk', 'missed', 'generation', 'search', 'tools', 'worldwide', 'contact',
                       'question', 'intern', 'classes', 'trust', 'ability', 'businesses', 'join', 'industry', 'response', 'you', 'using', 'work',              'based', 'grow', 'provide']

        profile_list = set()
        k = soup.body.findAll(text=allowed)
        for i in k:
            if len(i) < 60 and not any(x in i.lower() for x in not_allowed):
                profile_list.add(i.strip().upper())
        self.update_jobs(company, profile_list)

但是如果子字符串存在于not_allowed中,则也省略了一个字符串。请任何人帮帮忙。你知道吗


Tags: 字符串inselfrejobsnotfindprofile
2条回答

看起来你的正则表达式写错了。你的notallowed regex实际上是在寻找那些单词是行中唯一的项目。你知道吗

re.compile(r'^something_i_dont_like$')将匹配某个我不喜欢的项,如果它是行中唯一的项

如果你想省略一些东西,你需要做一个消极的展望

re.compile(r'^((?!something_i_dont_like).)*$')

正则表达式

^ability$

意思是“该行仅由“能力”一词组成”。如果需要子字符串,只需更改为

ability

如果你想省略“能力”这个词,而不是“残疾”,那么使用

\bability\b

相关问题 更多 >