如何过滤字符串列表中的关键字?

奥列斯特·托科文科(Orest Tokovenko)

我有一个字符串列表,这些字符串是我使用BeautifulSoup抓取的链接。我无法弄清楚如何仅返回包含单词“ The”的字符串。该解决方案可能使用Regex,但对我不起作用。

我试过了

for i in links_list:
     if re.match('^The', i) is not None:
        eps_only.append(i)

但我收到类似的错误

File "/opt/homebrew/Caskroom/miniconda/base/lib/python3.8/re.py", line 191, in match
    return _compile(pattern, flags).match(string)
TypeError: expected string or bytes-like object

该列表如下所示:

['index.html', 'seinfeld-scripts.html', 'episodes_oveview.html', 'seinfeld-characters.html', 'buy-seinfeld.html', 'http://addthis.com/bookmark.php?v=250&username=doctoroids', None, None, None, None, 'http://community.seinfeldscripts.com', 'buy-seinfeld.html', 'seinfeld-t-shirt.html', 'seinfeld-dvd.html', 'episodes_oveview.html', 'alpha.html', '    http://www.shareasale.com/r.cfm?u=439896&b=119192&m=16934&afftrack=seinfeldScriptsTop&urllink=search%2E80stees%2Ecom%2F%3Fcategory%3D80s%2BTV%26i%3D1%26theme%3DSeinfeld%26u1%3Dcategory%26u2%3Dtheme', ' TheSeinfeldChronicles.htm', ' TheStakeout.htm', ' TheRobbery.htm', ' MaleUnbonding.htm', ' TheStockTip.htm', ' TheExGirlfriend.htm', ' ThePonyRemark.htm', ' TheJacket.htm', ' ThePhoneMessage.htm', ' TheApartment.htm', ' TheStatue.htm', ' TheRevenge.htm', ' TheHeartAttack.htm', ' TheDeal.htm', ' TheBabyShower.htm', ' TheChineseRestaurant.htm', ' TheBusboy.htm', 'TheNote.html', ' TheTruth.htm', 'ThePen.html', ' TheDog.htm', ' TheLibrary.htm', ' TheParkingGarage.htm', 'TheCafe.html', ' TheTape.htm', 'TheNoseJob.html', 'TheStranded.html', ...]

更新:完整代码

import requests
import re
from bs4 import BeautifulSoup

##################
##--user agent--##
##################

user_agent_desktop = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '\
    'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 '\
    'Safari/537.36'

headers = {'User-Agent': user_agent_desktop}

#########################
##--fetching the page--##
#########################

URL = 'https://www.seinfeldscripts.com/seinfeld-scripts.html'
page = requests.get(URL, headers=headers)
soup = BeautifulSoup(page.content, 'html.parser')


############################################################
##--scraping the links to the scripts from the main page--##
############################################################

links_list = []
eps_only = []

for link in soup.find_all('a'):
    links_list.append(link.get('href'))

### sorting for links that contain 'the' ###

for i in filter(None, links_list):
    if re.match('^The', str(i)) is not None:
        eps_only.append(i)
        print(eps_only)
贾维斯

您应该过滤None从BeautifulSoup返回的列表元素(不带):

for i in filter(None, links_list):
    if re.match('^The', str(i)) is not None:
        eps_only.append(i)
print(eps_only)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

过滤以特定关键字开头的字符串列表

如何在字符串列表中搜索关键字,然后返回该字符串?

遍历字符串列表,查找关键字并打印

返回包含用户在弹性搜索中输入的关键字的字符串列表

返回列,其中包含字符串列中存在的关键字列表-Pandas

Pig:Python UDF在文本中搜索关键字/字符串列表

如果关键字在另一个列表中使用 Ramda 匹配,则过滤字符串列表

Java-在另一个字符串列表中搜索关键字列表

如何替换与python中关键字字符串列表匹配的列表列表中的值

如何根据字符串中的特定关键字对列表进行排序

如何在字符串中查找关键字

是否有任何机器人框架关键字来排序具有特殊字符的字符串列表?

如何将csv字符串转换为列并通过Excel中的特定关键字进行过滤?

在python中使用正则表达式在字符串列表中的匹配关键字之后找到下一个单词

Dart-如何强制将地图关键字包含在列表字符串中

如何根据关键字过滤整数,字符串和整数,带破折号的字符串和整数的关键字

在关键字包含特定字符串的python字典中过滤项目

在python中过滤字符串列表

从 Python 列表中过滤非英语关键字

如果字符串中存在列表中的任何关键字,则匹配

用关键字匹配列表分割字符串?

如何从Powershell中已过滤的列表中排除关键字

如何实现根据用户关键字进行搜索和过滤,以显示对象列表中的数据?

如何使用关键字列表(在其他表的列中)过滤Power BI表

如何根据关键字列表过滤Scala SQL上下文数据框中的文本

如何快速从不包含关键字列表中的关键字的列表中删除项目?

“ In”关键字无法检查元组列表中的字符串是否可用

从包含某些关键字的列表中删除多个字符串元素

如何过滤VBA中的关键字,包括可能找不到的关键字?