手写爬取静态页面汽车之家

scrapy写多了,手写爬虫有点生疏,今天来回顾手写爬取静态页面,以便日后做笔记用,我今天爬取的是汽车之家网页,
第一步:导入requests和bs4
import requests
from bs4 import BeautifulSoup

第二步:获取页面,我们用get发送请求,encoding是编码,apparent_encoding是页面编码获取的一种方式
response = requests.get(url='https://www.autohome.com.cn/news/')
content = response.content
response.encoding = response.apparent_encoding

第三步:解析页面,在这里我们用的BeautifulSoup和lxml来解析页面,首先定义一个soup对象,然后通过find去完成解析操作,代码如下
soup = BeautifulSoup(response.text,'lxml')
div = soup.find(name='div',id='auto-channel-lazyload-article')
li_list = div.find_all(name='li')
for li in li_list:
h3 = li.find(name='h3')
if not h3:
continue
# print(h3.text)

p = li.find(name='p')
a = li.find('a')
print(a.get('href'))
print(p.text)
print('-'*30)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值