爬虫(selenium)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import requests
from bs4 import BeautifulSoup

# 目标网址
url = "https://www.peakbagger.com/list.aspx?lid=5651"
headers = {
# 改成自己的请求头
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36 Edg/105.0.1343.42"
}

# 如果有奇怪报错则加上这句话
# requests.packages.urllib3.disable_warnings()
# 此时下面改成:
# resp = requests.get(url, headers=headers, verify=False)

resp = requests.get(url, headers=headers)
soup = BeautifulSoup(resp.content, 'html.parser')

# 示例:
.find(class_='gray')
.findAll('tr')

# 获取文本:
.find(class_='gray').text

# 退出
resp.close()