繁体   English   中英

Python将字符串添加到具有多个项目的匹配列表

[英]Python adding a string to a match list with multiple items

我正在处理的代码是从具有2个字段,URL和标题的HTML页面中检索列表...

URL始终以/URL....开头/URL....我需要在从re.findall返回的每个返回值后面附加“ http://website.com ”。

到目前为止的代码是这样的:

bsoup=bs(html)
tag=soup.find('div',{'class':'item'})
reg=re.compile('<a href="(.+?)" rel=".+?" title="(.+?)"')
links=re.findall(reg,str(tag))
*(append "http://website.com" to the href"(.+?)" field)*
return links

尝试:

for link in tag.find_all('a'):
    link['href'] = 'http://website.com' + link['href']

然后使用以下输出方法之一:

应用更改后, return str(soup)将为您提供文档。

return tag.find_all('a')获取所有链接元素。

return [str(i) for i in tag.find_all('a')]将所有链接元素转换为字符串。

现在,当您已经有XML解析器工作时 ,请勿尝试使用正则表达式解析HTML

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM