【发布时间】:2019-01-18 00:26:19
【问题描述】:
我想找到所有页面上的链接,这段代码只获取以http://开头的链接,但是大部分链接都是https://我该如何编辑你的代码在下面找到两者?
for link in soup.find_all('a',attrs={'href':re.compile("^http://")}):
import requests,bs4,re
res=requests.get('https://www.nytimes.com/2018/11/21/nyregion/president-trump-immigration-law-firms.html?action=click&module=Top%20Stories&pgtype=Homepage')
soup=bs4.BeautifulSoup(res.text,'html.parser')
x=[]
y=[]
z=[]
for link in soup.find_all('a',attrs={'href':re.compile("^http://")}):
print(link.get('href'))
x=link.get('href')
我知道我可以简单地获取所有链接,但我想同时获得 http:// 和 https:// find_all
for i in soup.select('a'):
print(i.get('href'))
【问题讨论】:
-
如何使用这个正则表达式
^(http|https)://.*。 ? -
或使用
^http*://[a-zA-z] -
如果要查找所有链接,为什么要过滤属性?
-
@Barmar 链接带有它们的文本和字体格式以及类似的东西
-
@Enix 你的编辑作品,如果你愿意,你可以发布作为答案
标签: python python-3.x beautifulsoup findall