find_all 具有多个属性
find_all with multiple attributes
我想在页面上找到所有链接,这段代码只获取以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'))
您想将 link 分类为 http 和 https 吗?使用 .startswith()
或 re.match()
找到它
http = []
https = []
for link in soup.find_all('a'):
url = link.get('href')
if url.startswith('http://'): # or: if re.match("^http://", url)
http.append(url)
else:
# should be https://
https.append(url)
print(https)
print(http)
您可以使用此正则表达式来匹配 http
或 https
:
^(http|https)://.*
正则表达式 (a|b)
表示:匹配模式 a
或 b
.
我想在页面上找到所有链接,这段代码只获取以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'))
您想将 link 分类为 http 和 https 吗?使用 .startswith()
或 re.match()
http = []
https = []
for link in soup.find_all('a'):
url = link.get('href')
if url.startswith('http://'): # or: if re.match("^http://", url)
http.append(url)
else:
# should be https://
https.append(url)
print(https)
print(http)
您可以使用此正则表达式来匹配 http
或 https
:
^(http|https)://.*
正则表达式 (a|b)
表示:匹配模式 a
或 b
.