Python将字符串添加到包含多个项的匹配列表中
我正在处理的代码是从一个HTML页面中检索一个列表,该页面包含两个字段、URL和标题 无论如何,URL以Python将字符串添加到包含多个项的匹配列表中,python,Python,我正在处理的代码是从一个HTML页面中检索一个列表,该页面包含两个字段、URL和标题 无论如何,URL以/URL….开头,我需要将“”附加到从re.findall返回的每个值 目前的准则是: bsoup=bs(html) tag=soup.find('div',{'class':'item'}) reg=re.compile('<a href="(.+?)" rel=".+?" title="(.+?)"') links=re.findall(reg,str(tag)) *(append
/URL….
开头,我需要将“”附加到从re.findall
返回的每个值
目前的准则是:
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
bsoup=bs(html)
tag=soup.find('div',{'class':'item'})
reg=重新编译('尝试:
然后使用以下输出方法之一:
return str(soup)
在应用更改后获取文档
return标记。find_all('a')
获取所有链接元素
return[str(i)for i in tag.find_all('a')]
获取所有转换为字符串的链接元素
现在,当XML解析器已经在工作时,不要试图用正则表达式解析HTML。请使用漂亮的汤来查找链接!@CrazyPython,除非你想召唤Cthulhu。@timgeb你永远不知道,他可能想召唤他。然后我们需要将它迁移到StackExchange怀疑论者或Worldbuilding…你是要接受答案还是帽子?这就像说“谢谢”,因为它会赢得声誉。哦,我的不好。URL附件的顺序颠倒了。
for link in tag.find_all('a'):
link['href'] = 'http://website.com' + link['href']