功能点击具体的书名、价格,他逐渐锁定了数据所在的标签类别和class名称。这是一个需要耐心和细心的“侦探”工作。
第二、三天:编写第一个爬虫脚本(京东)。
他先尝试抓取单页数据。代码大致如下:
import requests
from bs4 import BeautifulSoup
import pandas as pd
import time
headers = {'User-Agent': 'Mozilla/5.0...'} # 模拟浏览器请求头
url = 'https://search.jd.com/...初中数学 教辅...' # 搜索URL
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
books = []
for item in soup.find_all('div', class_='gl-i-wrap'): # 根据实际class调整
try:
title = item.find('div', class_='p-name').em.get_text(strip=True)
price = item.find('div', class_='p-price').strong.i.get_text()
shop = item.find('div', class_='p-shop').span.get_text(strip=True) if item.find('div', class_='p-shop') else '未知'
# 评价数有时在另一个标签里,需要更复杂的查找
commit = item.find('d
…。。本站若有图片广告属于第三方接入,非本站所为,广告内容与本站无关,不代表本站立场,请谨慎阅读。
Copyright © 2020 生存中文 All Rights Reserved.kk