python爬虫基本方法

import requests
from bs4 import BeautifulSoup
response = requests.get(url='http://www.ifeng.com/')#网页
# response.encoding = 'utf8'
response.encoding = response.apparent_encoding#编码问题
# print(response.text)
soup = BeautifulSoup(response.text,features='html.parser')#(lxml)#变为对象,第二个参数为模式
target = soup.find(id = 'turnRed')#找到id
obj = target.find_all('li')#id 下的所有li
# print(obj)
for i in obj:
    a = i.find('a')#找到所有a标签
    if a:
        print(a.attrs.get('href'))#找到属性
总结:
总结一:
1、requests
response = requests.get('URL')
response.content #返回字节流
response.enconding
response.aparent_encoding
response.status_code


2 soup = beautifulsoup('<html>...)',features = 'html.parser')
v1 = soup.find('div')
soup.find(id = 'il')
soup.find('div',id='il)
   
v2 = soup.find_all('div') #返回列表
obj = v1
obj = v[0]


obj.text #获取文本
obj.attrs #获取属性

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值