python3——爬虫(day1)

一、爬虫基本操作

1)爬虫

            — 定向     

           —非定向

2)请求一个网址:https://www.autohome.com.cn/news/(获取新闻文章的标题)

对于http访问的请求,本质是返回一个字符串。

 

1、下载页面:https://www.autohome.com.cn/news/

2、筛选:使用正则表达式

      /*******************************有可用的开源模块**********************************/

a、requests  (在cmd中输入pip3 install requests来安装requests)(安装方法,出处:CSDN无鞋童鞋https://blog.csdn.net/fx677588/article/details/56830929

       response = requests.get('https://www.autohome.com.cn/news/')

       response.text

b、beautisoup模块   ((在cmd中输入pip3 install beautisoup4来安装beautisoup4))

/********

——安装后会报错ModuleNotFoundError: No module named 'bs4',解决方法

——https://blog.csdn.net/sinat_34937826/article/details/79992728

********/

       soup = BeautiSoup(response.text,features='html.parser')       //创建一个beautisoup对象,此时会把整个html转换成一个一个的项。对象嵌套对象

       //由于对象可以设置方法,方法里可以写正则表达式

      target = soup.find(id='auto-channel-lazyload-article')

       print(tartget)

       

from bs4 import BeautifulSoup
import requests

response=requests.get('https://www.autohome.com.cn/news/')

soup = BeautifulSoup(response.text,features='html.parser')
target=soup.find(id='auto-channel-lazyload-article')

print(target)

 

结果截图:

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值