python爬虫ajax爬取代码
时间: 2023-12-25 10:05:25 浏览: 104
以下是一个使用Python爬虫爬取Ajax请求数据的示例代码:
```python
import requests
# 发送Ajax请求并获取数据
def get_ajax_data(url):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
'Referer': 'https://www.example.com' # 根据实际情况设置Referer
}
response = requests.get(url, headers=headers)
data = response.json() # 解析JSON数据
return data
# 下载图片
def download_image(url, filename):
response = requests.get(url)
with open(filename, 'wb') as f:
f.write(response.content)
# 主函数
def main():
# 发送Ajax请求并获取数据
ajax_url = 'https://www.example.com/ajax'
data = get_ajax_data(ajax_url)
# 解析数据并下载图片
for item in data:
image_url = item['image_url']
image_filename = item['image_filename']
download_image(image_url, image_filename)
if __name__ == '__main__':
main()
```
请注意,上述代码仅为示例,实际使用时需要根据具体的网站和Ajax请求进行相应的修改。
阅读全文