import requests
from bs4 import BeautifulSoup
def get_url(url):
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.0.0'}
r = requests.get(url, headers = headers, timeout = 30)
r.encoding = r.apparent_encoding
html = r.text
soup = BeautifulSoup(html, 'html.parser')
#print(soup.prettify())
a = soup.find_all('a')
for i in a:
#print(i)
try:
#print(i.h3.attrs['title'], '\n', 'http:'+i.attrs['href'], '\n')
print(i.string, '\n', i.attrs['href'], '\n')
except:
pass
if __name__ == '__main__':
url = input()
get_url(url)
爬取所有超链接
最新推荐文章于 2024-09-10 13:01:50 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)