提取网页中的链接:Python 代码示例
以下是使用 Python 提取给定链接中包含 '/post/' 的链接的示例代码:
import requests
from bs4 import BeautifulSoup
url = 'https://coomer.party/onlyfans/user/charlotteniya?o=0'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36'
}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.content, 'html.parser')
links = soup.find_all('a', href=True)
post_links = []
for link in links:
if '/post/' in link['href']:
post_links.append(link['href'])
print(post_links)
请注意,此代码使用了 requests 库进行网页请求,并使用了 BeautifulSoup 库来解析网页内容。同时,还添加了一个自定义的请求头 headers,以模拟浏览器发送请求。
原文地址: https://www.cveoy.top/t/topic/qlc5 著作权归作者所有。请勿转载和采集!