本文最后更新于841 天前,其中的信息可能已经过时,如有错误请评论留言
下面是爬取书的价格和书名的简单示例:
import requests
from bs4 import BeautifulSoup
content = requests.get("http://books.toscrape.com/").text # 拿到网页源码
soup = BeautifulSoup(content, "html.parser")
all_prices = soup.find_all("p", attrs={"class": "price_color"}) # 获取所有类名为“price_color”的 p 标签
for price in all_prices:
print(price.string) #拿到每个标签里的文字内容
all_titles = soup.find_all("h3")
for title in all_titles:
all_links = title.find_all("a")
for link in all_links:
print(link.string)
如果h3里只有一个a标签,可以用find()代替find_all(),里面也不需要循环了,如下:
for title in all_titles:
link = title.find("a")
print(link.string)
再来一个爬虫经典案例,爬豆瓣电影TOP250的影片名,完整代码如下:
import requests
from bs4 import BeautifulSoup
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36"
}
for i in range(0, 250, 25):
response = requests.get(f'https://movie.douban.com/top250?start={i}', headers=headers)
html = response.text
soup = BeautifulSoup(html, 'html.parser')
all_titles = soup.find_all('span', attrs={'class': 'title'})
for title in all_titles:
title_string = title.string
if '/' not in title_string:
print(title_string)
下面来进阶一点,我们来爬一下图片
为了安全就不做过多解释了
import requests
from bs4 import BeautifulSoup
from urllib.parse import urljoin
headers = {
'Referer': 'https://www.pixiv.net/',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36 Edg/123.0.0.0'
}
url = 'https://www.toopic.cn/'
path = 'F://spiderImages'
response = requests.get(url, headers=headers)
html = response.text
soup = BeautifulSoup(html, 'html.parser')
image_links = []
for img in soup.find_all('img', attrs={'class': 'lazy'}):
img_url = img['data-original']
# 如果图片链接是相对路径,则将其转换为绝对路径
if not img_url.startswith(('http://', 'https://')):
img_url = urljoin(url, img_url)
image_links.append(img_url)
for i, image_link in enumerate(image_links):
response = requests.get(image_link, headers=headers)
with open(f'{path}/img{i + 1}.jpg', 'wb') as file:
file.write(response.content)