试图用正确的关键字收集此页上的特定链接,目前为止我有:
from bs4 import BeautifulSoup
import random
url = 'http://www.thenextdoor.fr/en/4_adidas-originals'
r = requests.get(url)
soup = BeautifulSoup(r.text, 'lxml')
raw = soup.findAll('a', {'class':'add_to_compare'})
links = raw['href']
keyword1 = 'adidas'
keyword2 = 'thenextdoor'
keyword3 = 'uncaged'
for link in links:
text = link.text
if keyword1 in text and keyword2 in text and keyword3 in text:
我试图提取this link
最佳答案
您可以检查是否所有都存在于all()
中,以及1是否存在于any()
中。
from bs4 import BeautifulSoup
import requests
res = requests.get("http://www.thenextdoor.fr/en/4_adidas-originals").content
soup = BeautifulSoup(res)
atags = soup.find_all('a', {'class':'add_to_compare'})
links = [atag['href'] for atag in atags]
keywords = ['adidas', 'thenextdoor', 'Uncaged']
for link in links:
if all(keyword in link for keyword in keywords):
print link
输出:
http://www.thenextdoor.fr/en/clothing/2042-adidas-originals-Ultraboost-Uncaged-2303002052017.html
http://www.thenextdoor.fr/en/clothing/2042-adidas-originals-Ultraboost-Uncaged-2303002052017.html
关于python - 搜寻href连结,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41645998/