【发布时间】:2020-02-20 22:21:28
【问题描述】:
我有一个代码可以查找某些 URL 的信息 我怎样才能设置这个代码只给我一个字符串结果? 实际上这段代码给我带来了所有的结果,我只需要一个。 在下面的图像中,绿色矩形是正确的结果,但如果 url 多次包含该字符串,这将显示两个,红色矩形。
for idx,row in df.iterrows():
url = row['e.URL'].replace('/v01/', '/depot/')
x = urlopen(url)
new = x.read()
soup = BeautifulSoup(new, "lxml-xml")
match = ''.join(re.findall(r"(?i)cl[a-zA-Z]{3}\d{5}", str(soup)))
df.at[idx,'NEW_APP'] = match
下面的代码给我带来了所有的结果:
match = ''.join(re.findall(r"(?i)cl[a-zA-Z]{3}\d{5}", str(soup)))
参考下图:
【问题讨论】:
-
给我以下错误:
-
----> 6 匹配 = ''.join(re.search(r"(?i)cl[a-zA-Z]{3}\d{5}", str (soup))) TypeError: can only join an iterable
-
你不能在任何东西上加入一个字符串。你只会退回比赛吗?
match = re.search(...) -
现在我这样设置代码:match = (re.search(r"(?i)cl[a-zA-Z]{3}\d{5}", str(soup ))) 但结果是这样的,我只需要代码:
标签: python regex string pandas