以下是使用Python和requests库、BeautifulSoup库爬取微博的代码,可以爬取关键字为“甲流”的前100页微博:
```python
import requests
from bs4 import BeautifulSoup
keyword = '甲流'
page_num = 100
for page in range(1, page_num+1):
url = f'https://s.weibo.com/weibo?q={keyword}&typeall=1&suball=1×cope=custom:2022-01-01-0:2022-12-31-23&page={page}'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36'
}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
cards = soup.find_all('div', class_='card-wrap')
for card in cards:
# 获取微博内容和发布时间
content = card.find('p', class_='txt').text.strip()
time = card.find('p', class_='from').find_all('a')[-1].text.strip()
print(content, time)
```
以上代码会依次爬取每一页的微博,并输出每条微博的内容和发布时间。需要注意的是,由于微博有反爬虫机制,可能需要添加一些反反爬虫措施,例如设置请求头、使用代理IP等。