【学习】python爬虫简单例子

python搭建爬虫思维流程图

  1. 发送URL请求 response对象=request.get(URL)
  2. 提取文本 res=response对象.text
  3. html文件字符串解析 BS对象=BeautifulSoup(字符串, ‘html.parser’)
  4. find() 或 find_all() 函数返回所爬内容
  5. 遍历提取数据
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

练习1

爬取的是网上书店Books to Scrape中所有书的分类类型,并且将它们打印出来。
它的位置就在网页的左侧,如:Travel,Mystery,Historical Fiction…等。
网页的URL网址:http://books.toscrape.com/
在这里插入图片描述

import requests
from bs4 import BeautifulSoup
URL='http://books.toscrape.com/'
# 1. 发送URL请求
res=requests.get(URL)
print('requests.get返回'+str(type(res))+'类型的对象')
# 2. 提取文本(字符串)
html=res.text
print('res.text返回'+str(type(html))+'类型的对象')
# 3. 字符串解析
soup=BeautifulSoup(html, 'html.parser')
print('BeautifulSoup返回'+str(type(soup))+'类型的对象')
# 4. 检索所需内容
items=soup.find('ul', class_='nav').find('ul').find_all('li')
# 5. 遍历打印结果
print('Books to Scrape中图书分类类型表:\n')
for item in items:
    print(item.text.strip())

练习2

题目要求:爬取的是网上书店Books to Scrape Travel这类书中,所有书的书名、评分、价格三种信息,并且打印提取到的信息。
网页URL:
http://books.toscrape.com/catalogue/category/books/travel_2/index.html

import requests
from bs4 import BeautifulSoup
URL='http://books.toscrape.com/catalogue/category/books/travel_2/index.html'
# 1. 发送URL请求
res=requests.get(URL)
print('requests.get返回'+str(type(res))+'类型的对象')
# 2. 提取文本(字符串)
html=res.text
print('res.text返回'+str(type(html))+'类型的对象')
# 3. 字符串解析
soup=BeautifulSoup(html, 'html.parser')
print('BeautifulSoup返回'+str(type(soup))+'类型的对象')
# 4. 检索所需内容
#items=soup.find('ul', class_='nav').find('ul').find_all('li')
x1=soup.find_all(class_='product_pod')
print(type(x1))
for item in x1:
    book_name=item.find('h3').find('a')
    book_price=item.find('div', class_='product_price').find('p', class_='price_color')
    book_rating=item.find('p')
    print('Title:'+book_name['title']+'\n','Price:'+book_price.text.strip()+'\n',book_rating['class']) 

练习3

题目要求:你需要爬取的是博客人人都是蜘蛛侠,首页的四篇文章信息,并且打印提取到的信息。

提取每篇文章的:

文章标题
发布时间
文章链接
网页URL:
https://wordpress-edu-3autumn.localprod.oc.forchange.cn/

import requests
from bs4 import BeautifulSoup
URL='https://wordpress-edu-3autumn.localprod.oc.forchange.cn/'
res=requests.get(URL)
html=res.text
soup=BeautifulSoup(html, 'html.parser')
items=soup.find_all('article')
for item in items:
    book_title=item.find('h2', class_='entry-title')
    book_ref=item.find('a')
    release_time=item.find('div', class_='entry-meta')
    print(book_title.text+'\n',book_ref['href'], release_time.text)
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值