gpt4 book ai didi

python - 抓取 href 链接

转载 作者:数据小太阳 更新时间:2023-10-29 02:25:46 28 4
gpt4 key购买 nike

尝试使用正确的关键字收集此页面上的特定链接,到目前为止我有:

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() 检查是否所有都存在,以及是否存在 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com