python逆向分析获取数据的代码,目标:1.获取“新书推荐”图书名称;2.获取图书ID;3.获取图书照片网址。
时间: 2024-06-11 17:10:43 浏览: 84
由于没有提供具体的网站或数据源,以下代码仅作为示例,具体实现需要根据具体情况进行调整。
1. 获取“新书推荐”图书名称
```python
import requests
from bs4 import BeautifulSoup
url = 'http://example.com/new_books'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
book_names = []
book_divs = soup.find_all('div', class_='book')
for book_div in book_divs:
name = book_div.find('h2').text.strip()
book_names.append(name)
print(book_names)
```
2. 获取图书ID
```python
import requests
from bs4 import BeautifulSoup
url = 'http://example.com/new_books'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
book_ids = []
book_divs = soup.find_all('div', class_='book')
for book_div in book_divs:
id = book_div.get('data-id')
book_ids.append(id)
print(book_ids)
```
3. 获取图书照片网址
```python
import requests
from bs4 import BeautifulSoup
url = 'http://example.com/new_books'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
book_photos = []
book_divs = soup.find_all('div', class_='book')
for book_div in book_divs:
photo_url = book_div.find('img').get('src')
book_photos.append(photo_url)
print(book_photos)
```
阅读全文