Python之爬虫小人生

先运行Jupyter notebook 

  • 于CMD中输入

可于浏览器中自动开启Jupyter notebook的页面

点选右上角的python3新建档案

到这里就是正式的进入到可撰写Python code(Ctrl + Enter编译)


学习程式语言的第一步Hello world!

将Python套件import进来

import requests
from bs4 import BeautifulSoup 

将网页Get下来

import requests
from bs4 import BeautifulSoup

r = requests.get("https://www.ptt.cc/bbs/MobileComm/index.html") #将此页面的HTML GET下来
print(r.text) #印出HTML

将抓下来的资料用Beautifulsoup4转为HTML的parser

import requests
from bs4 import BeautifulSoup

r = requests.get("https://www.ptt.cc/bbs/MobileComm/index.html") #将网页资料GET下来
soup = BeautifulSoup(r.text,"html.parser") #将网页资料以html.parser
sel = soup.select("div.title a") #取HTML标中的 <div class="title"></div> 中的<a>标签存入sel

因为我想选取的是网页里的文章标题,所以soup.select中放的才是div.title a

<div class="title">	
    <a href="/bbs/MobileComm/M.1539248247.A.3CF.html">[问题]Pixel3 / XR / XZ3 选择? </a>		
</div>

最后写一个回圈将爬下来的文章标题印出来

for s in sel:
    print(s["href"], s.text) 

完整的code与展示

import requests
from bs4 import BeautifulSoup

r = requests.get("https://www.ptt.cc/bbs/MobileComm/index.html") #将网页资料GET下来
soup = BeautifulSoup(r.text,"html.parser") #将网页资料以html.parser
sel = soup.select("div.title a") #取HTML标中的 <div class="title"></div> 中的<a>标签存入sel
for s in sel:
    print(s["href"], s.text)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值