1. 程式人生 > >python3.6 requests+re+BeautifulSoup的使用

python3.6 requests+re+BeautifulSoup的使用

requests是python的一個http請求模組,常用的請求方式有GET和POST GET是向伺服器傳送請求,伺服器返回請求的響應資料;POST是向伺服器提交資料,比如向伺服器提交的使用者登陸表單,就是POST請求,當然為何不用GET提交登陸請求,這是使用者資訊隱私的問題。 話不多說,直接上程式碼

# -*- conding:utf-8 -*-
import requests
import re
from bs4 import BeautifulSoup
headers = {"User-Agent:"
           "Mozilla/5.0 (Windows NT 6.3;WOW64; rv:41.0)Gecko/20100101 Firefox/42.1"}
for page in range(10):
    print("============正在抓取第"+str(page+1)+"頁======")
    num = page*25
    url = "https://movie.douban.com//top250?start=%s&filter="%num
    html = requests.get(url)
    soup = BeautifulSoup(html.text,"html.parser")
    soup = str(soup)
    title = re.compile(r'<span class="title">(.*)?</span>')
    names = re.findall(title,soup)
    for name in names:
        if name.find('/'):
            print(name)
print("抓取完畢!")