python爬取英为财情美股企业列表
时间: 2023-08-20 10:08:11 浏览: 283
可以使用Python中的requests和BeautifulSoup库来爬取英为财情美股企业列表。以下是一个简单的示例代码:
```python
import requests
from bs4 import BeautifulSoup
url = 'https://www.investing.com/equities/StocksFilter?index_id=17'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
table = soup.find('table', {'id': 'cross_rate_markets_stocks_1'})
rows = table.find_all('tr')
for row in rows:
cols = row.find_all('td')
if cols:
name = cols[1].text.strip()
symbol = cols[2].text.strip()
print(symbol, name)
```
该代码会爬取英为财情中的美股企业列表(https://www.investing.com/equities/StocksFilter?index_id=17),并输出每家企业的股票代码和名称。请注意,该网站有反爬虫机制,需要设置headers模拟浏览器访问,否则会被网站禁止访问。
阅读全文