Python爬虫入门学习--(单线程爬虫)

单线程爬虫最基本原理:使用Requests获取网页源代码,再使用正则表达式匹配出感兴趣的内容

第一个网页爬虫—Requests获取网页源代码

  • 直接获取源代码
  • 修改http头获取源代码

不需要header

#-*-coding:utf8-*-
import requests
html = requests.get('http://tieba.baidu.com/f?ie=utf-8&kw=python')
print html.text

需要header

#-*-coding:utf8-*-

import requests
import re
import sys

reload(sys)
sys.setdefaultencoding("gb18030")
type = sys.getfilesystemencoding()

# headers = {}
html = requests.get('http://jp.tingroom.com/yuedu/yd300p/')
# html = requests.get('http://jp.tingroom.com/yuedu/yd300p/',headers = headers)

html.encoding = 'utf-8'
# print html.text

title = re.findall('color:#666666;">(.*?)</span>',html.text,re.S)
for each in title:
   print each

chinese = re.findall('color: #039;">(.*?)</a>',html.text,re.S)
for each in chinese:
    print each

怎么样获取header

在网页中点击审查元素,然后点击network,然后再刷新网页,随便点一个,再点击header,往下翻就可以看到
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值