網絡爬蟲練習

1.利用requests.get(url)獲取網頁頁面的html文件

import requests
newsurl='http://news.gzcc.cn/html/xiaoyuanxinwen/'
res = requests.get(newsurl) #返回response對象
res.encoding='utf-8'
print(res.text)

?

2.利用BeautifulSoup的HTML解析器,生成結構樹

import requests
from bs4 import BeautifulSoup
newsurl='http://news.gzcc.cn/html/xiaoyuanxinwen/'
res = requests.get(newsurl) #返回response對象
res.encoding='utf-8'
print(BeautifulSoup(res.text,'html.parser'))

3.找出特定標簽的html元素

soup.p #標簽名,返回第一個

soup.head

soup.p.name #字符串

soup.p. attrs #字典,標簽的所有屬性

soup.p. contents # 列表,所有子標簽

soup.p.text #字符串

soup.p.string

soup.select(‘li')

?

4.取得含有特定CSS屬性的元素

soup.select('#p1Node')

soup.select('.news-list-title')

?

5.練習:

取出h1標簽的文本

import requests
from bs4 import BeautifulSoup
url = 'http://news.gzcc.cn/html/xiaoyuanxinwen/'
res = requests.get(url)
res.encoding = 'utf-8'
soup = BeautifulSoup(res.text,'html.parser')
#取h4標簽的文本
print(soup.select('h4')[0].text)

?

取出a標簽的鏈接

import requests
from bs4 import BeautifulSoup
url = 'http://news.gzcc.cn/html/xiaoyuanxinwen/'
res = requests.get(url)
res.encoding = 'utf-8'
soup = BeautifulSoup(res.text,'html.parser')
#取a標簽的鏈接
for i in soup.select('a'):print(i.get('href'))

?

取出所有li標簽的所有內容

import requests
from bs4 import BeautifulSoup
url = 'http://news.gzcc.cn/html/xiaoyuanxinwen/'
res = requests.get(url)
res.encoding = 'utf-8'
soup = BeautifulSoup(res.text,'html.parser')
#取li標簽的所有內容
for i in soup.select('li'):print(i)

?


取出一條新聞的標題、鏈接、發布時間、來源

import requests
from bs4 import BeautifulSoup
url = 'http://news.gzcc.cn/html/xiaoyuanxinwen/'
res = requests.get(url)
res.encoding = 'utf-8'
soup = BeautifulSoup(res.text,'html.parser')# 取出一條新聞的標題、鏈接、發布時間、來源
print(soup.select('.news-list-title')[0].text)
print(soup.select('li')[1].a.attrs['href'])
print(soup.select('.news-list-info')[0].contents[0].text)
print(soup.select('.news-list-info')[0].contents[1].text)

?

轉載于:https://www.cnblogs.com/weixingna/p/8670551.html

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

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

相關文章

BZOJ 3434 時空穿梭

題目鏈接:http://www.lydsy.com/JudgeOnline/problem.php?id3434 題意: 思路: const int mod10007; const int N100005;int g[22][N]; int C[N][22],mou[N]; int h[22][N][13];int prime[N],cnt; int tag[N];void init() {int i,j;mou[1]1;f…

powershell /遍歷/psobject/字符串轉換Json/json數組操作

讀取json文檔 $json (Get-Content "C:\HDDList.json" -Raw) | ConvertFrom-Json $select$json.Content.selected[0] $HddCoun$json.Content.normal.Count 遍歷數組 foreach($str in $json.versions) { echo "value: $str" } json數據轉換psobject.pro…

plex實現流媒體服務器_如何從Plex Media Server離線查看下載和同步媒體

plex實現流媒體服務器Streaming content from your Plex Media Server is great, but sometimes—like when you’re going to be offline or stuck with cruddy internet speeds while traveling—there’s no substitution for having a copy of the media stored on your de…

.NET Conf 2022?大會日程全曝光!!前沿、硬核、創意.....精彩就等你來!!

倒計時2天一場規模宏大,內容硬核,大咖齊聚的.NET 領域年度最大的盛會即將開幕.NET Conf 2022 12月3日-12月4日開源 安全 賦能誠邀您的加入立即掃碼預約加入.NET年度盛宴!!.NET Conf China 2022.NET Conf China 2022是面向開發人員…

SpringMVC 數據的格式化、JSR 303數據校驗和國際化

SpringMVC 數據的格式化、JSR 303數據校驗和國際化轉載于:https://www.cnblogs.com/lusufei/p/7400963.html

C# 對程序窗口進程和進程ID

獲取當前激活窗口(頂置) GetForegroundWindow() [DllImport("user32.dll")]public static extern IntPtr GetForegroundWindow();[DllImport("user32.dll", EntryPoint "GetWindowText")]public static extern int GetW…

Linux下SSH遠程連接斷開后讓程序繼續運行解決辦法

screen -S yourname #新建一個叫yourname的sessionscreen -r yourname #回到yourname這個sessionscreen -X -S [yourname # you want to kill]quit #刪除無用的screen,使用時不用加中括號 screen -ls #列出當前所有的session screen -d yourname #遠程detach某個ses…

網游的服務器瓶頸

1.服務器的數量。 2.服務器的I/O瓶頸。 3.游戲當中的突發性高并發量。 4.CPU主頻。

誰說.NET沒有GC調優,只改一行代碼就讓程序不再占用內存

經常看到有群友調侃“為什么搞Java的總在學習JVM調優?那是因為Java爛!我們.NET就不需要搞這些!”真的是這樣嗎?今天我就用一個案例來分析一下。昨天,一位學生問了我一個問題:他建了一個默認的ASP.NET Core …

wmi服務或wmi提供程序_什么是WMI提供程序主機(WmiPrvSE.exe),為什么使用那么多的CPU?...

wmi服務或wmi提供程序The WMI Provider Host process is an important part of Windows, and often runs in the background. It allows other applications on your computer to request information about your system. This process shouldn’t normally use many system re…

C# 快捷鍵/hotkey簡單例子

1.導入dll [System.Runtime.InteropServices.DllImport("user32.dll")] //申明API函數public static extern bool RegisterHotKey(IntPtr hWnd, // handle to windowint id, // hot key identifieruint fsModifiers, // key-modifier optionsKeys vk // virtual-key …

POJ 3233

矩陣分治 注意不要用 (*this) 會改變原值 #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <cstdlib> using namespace std; int n, p, k; struct Matrix{int num[35][35];voi…

zookeeper和etcd有狀態服務部署

zookeeper和etcd有狀態服務部署實踐 docker etcd zookeeper kubernetes 4k 次閱讀 讀完需要 78 分鐘 0 一. 概述 kubernetes通過statefulset為zookeeper、etcd等這類有狀態的應用程序提供完善支持&#xff0c;statefulset具備以下特性&#xff1a; 為pod提供穩定的唯一…

正在創建系統還原點_如何使Windows在啟動時自動創建系統還原點

正在創建系統還原點By default, System Restore automatically creates a restore point once per week and also before major events like an app or driver installation. If you want even more protection, you can force Windows to create a restore point automaticall…

WinForm(十六)綁定

在WinForm中&#xff0c;有很多添加和修改數據的場景&#xff0c;一般的做法是當點擊“添加”按鈕時&#xff0c;收集各控件的值&#xff0c;然后賦值給實體類的各個屬性&#xff0c;然后再完成保存工作。在修改時&#xff0c;首先把實體的原值&#xff0c;一個個賦值給控件&am…

在ubuntu 16.04里使用python—scrapy將爬取到的數據存到mysql數據庫中的一些隨筆

一、將爬取的數據保存到mysql數據庫的代碼&#xff08;已經能將爬取的數據保存到json文件&#xff09; &#xff08;1&#xff09;編輯Pipeline.py文件 &#xff08;2&#xff09;編輯settings.py文件 二、將數據保存至mysql數據庫出現的問題 &#xff08;1&#xff09;在將數據…

powershell XML操作

1.直接加入xml結構 加入<title>是為了后續能直接添加其他node&#xff0c;否則&#xff0c;后續操作可能無法AppendChild $xml "<?xml version1.0 encodingUTF-8?><case><title>please check each point</title></case>"$xm…

十大經典排序算法(動圖演示)

轉自&#xff1a;https://www.cnblogs.com/onepixel/articles/7674659.html 0、算法概述 0.1 算法分類 十種常見排序算法可以分為兩大類&#xff1a; 非線性時間比較類排序&#xff1a;通過比較來決定元素間的相對次序&#xff0c;由于其時間復雜度不能突破O(nlogn)&#xff0c…

【Python】安裝配置Anaconda

優點&#xff1a;解決Python 庫依賴問題清華安裝鏡像https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/ 轉載于:https://www.cnblogs.com/Neo007/p/7419253.html

如何實現 WPF 視頻封面查看器

如何實現 WPF 視頻封面查看器控件名&#xff1a;NineGridView作 者&#xff1a;WPFDevelopersOrg - 驚鏵原文鏈接[1]&#xff1a;https://github.com/WPFDevelopersOrg/WPFDevelopers框架使用.NET40&#xff1b;Visual Studio 2019;實現視頻封面查看器NineGridView基于Grid實…