python爬虫爬取微信读书
时间: 2023-11-12 21:58:00 浏览: 502
可以使用Python的requests和BeautifulSoup库来爬取微信读书的数据。首先需要登录微信读书网页版,获取到cookie信息,然后通过requests库发送请求获取到需要的数据,最后使用BeautifulSoup库解析HTML页面,提取出需要的信息。
以下是一个简单的示例代码:
```python
import requests
from bs4 import BeautifulSoup
# 登录微信读书获取cookie信息
login_url = 'https://weread.qq.com/web/login'
data = {
'userName': 'your_username',
'passwd': 'your_password'
}
response = requests.post(login_url, data=data)
cookie = response.cookies.get_dict()
# 发送请求获取数据
book_url = 'https://weread.qq.com/web/book/1234567890123456'
headers = {
'Cookie': '; '.join([f'{key}={value}' for key, value in cookie.items()])
}
response = requests.get(book_url, headers=headers)
html = response.text
# 解析HTML页面提取信息
soup = BeautifulSoup(html, 'html.parser')
title = soup.find('h1', class_='wr_bookName').text
author = soup.find('a', class_='wr_authorName').text
intro = soup.find('div', class_='wr_bookIntro').text.strip()
print(title)
print(author)
print(intro)
```
阅读全文