1. 程式人生 > >初探:Python中使用request和BeautifulSoup庫進行網路爬蟲

初探:Python中使用request和BeautifulSoup庫進行網路爬蟲

說起網路爬蟲,Python中最底層的應該是urllib,但是語法結構有些繁瑣,需要使用正則。而使用request和BeautifulSoup庫進行網路爬蟲,發現這真的是web開發人員的福音。凡是懂一些前端知識的人來說,使用request和BeautifulSoup庫進行爬蟲,真的有一種開心而愉快的感覺。

requests 主要是一個封裝好了http功能的庫, 可以實現基本的http操作。

beautifulsoup 主要提供了對html, xml網頁的一個完美的解析方式, 實際上, 他將html中的tag 作為樹節點進行解析, 於是, 我們可以將一個html頁面看成是一顆樹結構。也就是利用DOM(Document Object Model)來進行內容的抓取。

獲得網頁原始碼:

import requests
res = requests.get('https://www.sina.com.cn/')
res.encoding = 'utf-8'
print(res.text)

獲得需要的內容:

# 獲得需要的內容
from bs4 import BeautifulSoup
html_sample = res.text
soup = BeautifulSoup(html_sample,'html.parser')
# print(soup.text)  #得到的是title標籤內的內容
# 使用select找出含有h1的元素
header = soup.select('h1')
print(header)  # 得到的的含有h1標籤的一個列表,要獲得單純的一個含h1的標籤,可使用header[0],要獲得其中的文字,可使用下面。若有很多,可使用for迴圈
print(header[0].text)  # 提取其中的文字

seelct的使用:

如果使用select 找出所有id為title 的元素:alike = soup.select(‘#title’)
如果使用select 找出所有class為link 的元素:
soup = BeautifulSoup(html_sample)
for link in soup.select(‘.link’):
    print(link)

例子:

使用select找出所有a tag的hrefl連結:
ainks = soup.select(‘a’)
for link in alinks:
    print(link[‘href’])
例子:
a = '<a href="#" qoo=123 abc=456>I am a link</a>'
soup2 = BeautifulSoup(a)
alinks = soup2.select('a')[0]
print(alinks['href'])

一個簡單的抓取“糗事百科”內容的例子:

import requests
from bs4 import BeautifulSoup
content = requests.get('https://www.qiushibaike.com/').content
soup = BeautifulSoup(content,'html.parser')
story = soup.select('.content span')
for p in story:
    print(p.text)

以上的例子都是抓取的文字內容,並且其都在Doc中,有些網頁內容是在js或者XHR中,要具體問題具體分析。

此文只是requests和Beautifulsoup初探,後續會繼續更文。