python3爬蟲學習筆記

文章目錄

  • python3的文本處理
  • jieba庫的使用
    • 統計hamlet.txt文本中高頻詞的個數
    • 統計三國演義任務高頻次數
  • 爬蟲
    • 爬取百度首頁
    • 爬取京東某手機頁面
  • BeautifulSoup
    • 使用request進行爬取,在使用 BeautifulSoup進行處理!擁有一個更好的排版
    • BeautifulSoup爬取百度首頁

原文記錄內容太多現進行摘錄和分類

python3的文本處理

jieba庫的使用

pip3 install jieba

統計hamlet.txt文本中高頻詞的個數

講解視頻

kou@ubuntu:~/python$ cat ClaHamlet.py 
#!/usr/bin/env python
# coding=utf-8#e10.1CalHamlet.py
def getText():txt = open("hamlet.txt", "r").read()txt = txt.lower()for ch in '!"#$%&()*+,-./:;<=>?@[\\]^_‘{|}~':txt = txt.replace(ch, " ")   #將文本中特殊字符替換為空格return txt
hamletTxt = getText()
words  = hamletTxt.split()
counts = {}
for word in words:			counts[word] = counts.get(word,0) + 1
items = list(counts.items())
items.sort(key=lambda x:x[1], reverse=True) 
for i in range(10):word, count = items[i]print ("{0:<10}{1:>5}".format(word, count))

統計三國演義任務高頻次數

#!/usr/bin/env python
# coding=utf-8#e10.1CalHamlet.py
def getText():txt = open("hamlet.txt", "r").read()txt = txt.lower()for ch in '!"#$%&()*+,-./:;<=>?@[\\]^_‘{|}~':txt = txt.replace(ch, " ")   #將文本中特殊字符替換為空格return txt
hamletTxt = getText()
words  = hamletTxt.split()
counts = {}
for word in words:			counts[word] = counts.get(word,0) + 1
items = list(counts.items())
items.sort(key=lambda x:x[1], reverse=True) 
for i in range(10):word, count = items[i]print ("{0:<10}{1:>5}".format(word, count))

爬蟲

學習資源是中國大學mooc的爬蟲課程。《嵩天老師》
下面寫幾個簡單的代碼!熟悉這幾個代碼的書寫以后基本可以完成需求!

爬取百度首頁

import requestsr = requests.get("https://www.baidu.com")
fo = open("baidu.txt", "w+")
r.encoding =  'utf-8'
str = r.text
line = fo.write( str )

爬取京東某手機頁面

import requests
url = "https://item.jd.com/2967929.html"
try:r = requests.get(url)r.raise_for_status()//如果不是200就會報錯r.encoding = r.apparent_encoding//轉utf-8格式print(r.text[:1000])//只有前1000行
except:print("False")fo.close()

BeautifulSoup

使用request進行爬取,在使用 BeautifulSoup進行處理!擁有一個更好的排版

fo = open("jingdong.md","w")url = "https://item.jd.com/2967929.html"
try:r = requests.get(url)r.encoding = r.apparent_encodingdemo = r.textsoup = BeautifulSoup(demo,"html.parser")fo.write(soup.prettify())fo.writelines(soup.prettify())
except:print("False")fo.close()

BeautifulSoup爬取百度首頁

fo = open("baidu.md","w")try:r = requests.get("https://www.baidu.com")r.encoding = r.apparent_encodingdemo = r.textsoup = BeautifulSoup(demo,"html.parser")fo.write(soup.prettify())fo.writelines(soup.prettify())
except:print("False")
fo.close()

附贈
爬蟲和python例子開源鏈接

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/382502.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/382502.shtml
英文地址,請注明出處:http://en.pswp.cn/news/382502.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

linux 線程學習初步01

線程的概念 進程與線程內核實現 通過函數clone實現的 ps -Lf pidLinux內核線程實現原理 同一個進程下的線程&#xff0c;共享該進程的內存區&#xff0c; 但是只有stack區域不共享。 線程共享資源 a.文件描述符表 b.每種信號的處理方式 c.當前工作目錄 d.用戶id和組id 線程…

python3字符串處理,高效切片

高級技巧&#xff1a;切片&#xff0c;迭代&#xff0c;列表&#xff0c;生成器 切片 L [Hello, World, !]print("-------1.一個一個取-------") print(L[0]) print(L[1]) print(L[2])print("-------2.開辟一個新列表把內容存進去-------") r [] for i…

linux線程學習初步02

殺死線程的函數 int pthread_cancel(pthread_t thread); 參數介紹&#xff1a;需要輸入的tid 返回值&#xff1a;識別返回 errno成功返回 0 被殺死的線程&#xff0c;退出狀態值為一個 #define PTHREAD_CANCELED((void *)-1)代碼案例&#xff1a; #include <stdio.h> #…

python的文件基本操作和文件指針

讀寫模式的基本操作 https://www.cnblogs.com/c-x-m/articles/7756498.html r,w,a r只讀模式【默認模式&#xff0c;文件必須存在&#xff0c;不存在則拋出異常】w只寫模式【不可讀&#xff1b;不存在則創建&#xff1b;存在則清空內容】a之追加寫模式【不可讀&#xff1b;不…

python3 將unicode轉中文

decrypted_str.encode(utf-8).decode(unicode_escape)

HTTP菜鳥教程速查手冊

HTTP協議&#xff08;HyperText Transfer Protocol&#xff0c;超文本傳輸協議&#xff09;是因特網上應用最為廣泛的一種網絡傳輸協議&#xff0c;所有的WWW文件都必須遵守這個標準。 HTTP是一個基于TCP/IP通信協議來傳遞數據&#xff08;HTML 文件, 圖片文件, 查詢結果等&am…

mysql學習筆記01-創建數據庫

創建數據庫&#xff1a; 校驗規則&#xff1a;是指表的排序規則和查詢時候的規則 utf8_general_ci 支持中文&#xff0c; 且不區分大小寫 utf8_bin 支持中文&#xff0c; 區分大小寫 比如&#xff1a; create database db3 character set utf8 collate utf8_general_ci; &…

python的Web編程

首先看一下效果 完整代碼 import socket from multiprocessing import ProcessHTML_ROOT_DIR ""def handle_client(client_socket):request_data client_socket.recv(1024)print("request data:", request_data)response_start_line "HTTP/1.0 20…

mysql 學習筆記 02創建表

表結構的創建 比如&#xff1a; create table userinfo (id int unsigned comment id號name varchar(60) comment 用戶名password char(32),birthday date ) character set utf8 engine MyISAM;comment 表示注釋的意思 不同的存儲引擎&#xff0c;創建的表的文件不一樣

mysql 學習筆記03 常用數據類型

數值類型&#xff1a; a. 整數類型&#xff1a; 注意事項&#xff1a; 舉例&#xff1a;某個整型字段 &#xff0c;不為空&#xff0c;且有默認值 create table test (age int unisigned not null default 1);zerofill的使用 b. bit類型的使用 c.小數類型 小數類型占用…

VMware的虛擬機連不上網

1.如果你發現在VMware下運行的虛擬機無法連接網絡&#xff0c;那下面的方法也許可以幫 到你。&#xff08;前提是你的物理網絡是通的&#xff09; 第一步&#xff1a;在VMware界面下 單擊“編輯“→”虛擬網絡編輯器” 第二步&#xff1a;單擊”更改設置” 獲取權限&#xff…

python三國演義人物出場統計

完整代碼 開源代碼 統計三國演義人物高頻次數 #!/usr/bin/env python # codingutf-8 #e10.4CalThreeKingdoms.py import jieba excludes {"來到","人馬","領兵","將軍","卻說","荊州","二人","…

mysql 學習筆記03修改表以及其他操作

首先創建一張表 在現有表的結構上增加字段 alter table users add image varchar(100) not null defalut comment 圖片路徑;修改某個字段的長度 alter table users modify job vachar(60) not null comment 工作;刪除某個字段 刪除sex這個字段 alter table users drop se…

統計哈姆雷特文本中高頻詞的個數

統計哈姆雷特文本中高頻詞的個數 三國演義人物出場統計 開源代碼 講解視頻 kouubuntu:~/python$ cat ClaHamlet.py #!/usr/bin/env python # codingutf-8#e10.1CalHamlet.py def getText():txt open("hamlet.txt", "r").read()txt txt.lower()for ch…

mysql 學習筆記04 insert與update語句

1.插入數據 注意事項&#xff1a; 字符和日期類型&#xff0c; 要用 單引號 括起來 insert into (), (), () 例如&#xff1a; insert into goods values(1, abc, 2.2), (2, def, 3.3);這種形式添加多條記錄 insert 語句&#xff0c;如果沒有指定字段名&#xff0c;則values …

PyCharm怎么關閉端口,解決端口占用問題

在進行web開發遇到這個問題&#xff01;

mysql 筆記05 select語句以及條件語句的使用

select語句 過濾重復語句&#xff08;distinct&#xff09; 舉例&#xff1a; 查詢學生的總分 select name, math English China as 總分 from students;在姓趙的學生總分基礎上&#xff0c; 增加60%&#xff0c; select name, round((math English China) * 1.6, 2) as …

python3 與 Django 連接數據庫:Error loading MySQLdb module: No module named 'MySQLdb'

解決方法&#xff1a;在 init.py 文件中添加以下代碼即可。 import pymysql pymysql.install_as_MySQLdb()

mysql 學習筆記05 統計函數的相關使用

合計函數count&#xff0c; 統計多少條記錄 統計共有多少學生 select count(*) from students;查詢數學成績大于等于90的學生數量 select count(*) from students where math > 90;查詢總分超過235分的學生的數量 select count(*) from students where (English math Ch…