用python写一个麦当劳麦乐送门店爬虫
时间: 2023-06-30 21:23:19 浏览: 353
好的,以下是一个简单的用Python写的麦当劳麦乐送门店爬虫:
```python
import requests
from bs4 import BeautifulSoup
url = 'https://www.mcdelivery.com.cn/cn/browse/menu.html'
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
# 找到所有门店的链接
store_links = soup.find_all('a', {'class': 'store-filter__store-link'})
for store_link in store_links:
store_url = store_link.get('href')
store_name = store_link.get_text()
store_response = requests.get(store_url)
store_soup = BeautifulSoup(store_response.content, 'html.parser')
store_address = store_soup.find('div', {'class': 'store-info__address'}).get_text().strip()
print(f'{store_name}的地址是{store_address}')
```
这个爬虫首先访问麦当劳麦乐送的门店页面,然后用BeautifulSoup解析页面内容,找到所有门店的链接。接着,遍历每个门店的链接,访问门店页面,解析门店地址,并输出门店名称和地址。
需要注意的是,这个爬虫可能会被麦当劳网站的反爬虫机制所阻挡,如果你需要更高效、更稳定的爬虫,可以考虑使用一些反反爬虫技巧,比如使用代理IP、随机请求头等。