我有一个字符串列表,这些字符串是我使用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] 删除。
我来说两句