pachongwanchen
This commit is contained in:
30
1243/爬虫.py.txt
Normal file
30
1243/爬虫.py.txt
Normal file
@@ -0,0 +1,30 @@
|
||||
import requests
|
||||
from bs4 import BeautifulSoup as bs
|
||||
headers = {
|
||||
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
|
||||
}
|
||||
|
||||
url = 'https://www.baidu.com'
|
||||
params = {'key':'value'}
|
||||
|
||||
try:
|
||||
response = requests.get(url, params=params, headers=headers, timeout=5)
|
||||
response.raise_for_status()
|
||||
print("״̬<D7B4><CCAC>:", response.status_code)
|
||||
print('-'*20)
|
||||
|
||||
html_content = response.text
|
||||
soup = bs(html_content, 'lxml')
|
||||
title = soup.find('title').string
|
||||
print("ҳ<><D2B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>:", title)
|
||||
print('='*20)
|
||||
links = soup.find_all('a')
|
||||
for link in links:
|
||||
href = link.get('href')
|
||||
if href:
|
||||
print("<22><><EFBFBD><EFBFBD>:", href)
|
||||
print('='*20)
|
||||
except requests.exceptions.RequestException as e:
|
||||
print(f"<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: {e}")
|
||||
except Exception as e:
|
||||
print(f"<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: {e}")
|
||||
Reference in New Issue
Block a user