python爬蟲整理(包含例項)

2022-03-22 16:18:08 字數 1289 閱讀 5430

提取碼:po9s

pip install requests
response = requests.post(url, data=) #傳送post請求,請求url位址對應的響應

response.content.decode()

response.requests.headers #請求頭

response.headers #響應頭

response.requests.url #傳送請求的位址

response.url #響應位址

當不新增headers的時候會發現返回的內容只有一段,但是在新增了headers後就會發現返回的是整個網頁的html

headers = 

requests.get(url, headers=headers)

pip install retrying

from retrying import retry

@retry(stop_max_attempt_number=3)

def fun1():

print("this is func1")

raise valueerror("this is a test error")

headers =
2. cookie字典傳給cookies引數

- requests.get(url, cookie=cookie_dict) #注意是字典而不是上面的字串

json.laods

json.dumps

豆瓣電視爬蟲案例

xpath語法

2.//tag:能夠從任意節點開始選擇

3.@符號的用途

4.獲取文字:

5.當前

lxml

from lxml import etree

element = etree.html("html字串")

element.xpath("")

豆瓣電影爬蟲案例

字典推導式

# 

".format(i):10 for i in range(10)} #

三元運算子

糗事百科爬蟲案例

2.傳送請求獲取響應

3.提取資料

4.儲存

Python爬蟲例項

中國大學排名專案 功能描述 輸出 大學排名資訊的螢幕輸出 排名,大學名稱,總分 技術路線 requests bs4 定向爬蟲 僅對輸入url進行爬取,不擴充套件爬取 程式的結構設計 步驟1 從網路上獲取大學排名網頁內容 步驟2 提取網頁內容中資訊到合適的資料結構 二維列表 步驟3 利用資料結構展示並...

python 爬蟲例項

coding utf 8 import re import sys import os from time import sleep from bs4 import beautifulsoup import requests reload sys sys.setdefaultencoding utf...

Python 爬蟲例項

下面是我寫的乙個簡單爬蟲例項 1.定義函式讀取html網頁的源 2.從源 通過正規表示式挑選出自己需要獲取的內容 3.序列中的htm依次寫到d盤 usr bin python import re import urllib.request 定義函式讀取html網頁的源 def gethtml url...