【发布时间】:2020-02-11 22:46:34
【问题描述】:
我正在从 Github 中抓取 repo 的名称,例如:
repositorys = []
for ulr in user_repo_url: # in this list I have url like ('https://github.com/USER/?tab=repositories)
source = urllib.request.urlopen(url).read()
soup = bs.BeautifulSoup(source,'lxml')
repos = [repo.text for repo in soup.find_all('div',class_='d-inline-block mb-1')]
repositorys.append(repos)
return render(request,'file.html',{'repositorys':repositorys})
我正在使用 Django 并且一切正常,但是为了获得明文,我得到了名称和 '\n' 符号。我正在尝试使用条带和地图功能,但它们不起作用。您还有其他建议为什么它不起作用?
【问题讨论】:
-
repo.text.strip()? BS 也有repo.get_text(strip=True)来删除一些元素之间的\n。 -
@furas 是的 - 这很奇怪,但它仅适用于硬编码 URL,并且仅当此 URL 未存储在列表中时才有效
-
这能回答你的问题吗? Remove all newlines from inside a string