python網路爬蟲之新手胡亂玩

2021-08-19 04:03:55 字數 2094 閱讀 5942

抓學校官網新聞

import requests

from bs4 import beautifulsoup

#**大學綜合新聞頁面中所有新聞

with open('d:/抓的資料/**綜合新聞.txt','w',encoding='utf-8') as f:

f.write('**大學綜合新聞頁面所有新聞如下:\n')

res = requests.get('不告訴你')

res.encoding='gb2312'

soup =beautifulsoup(res.text,'html.parser')

news=soup.select('.newsgridlist')

for xiaoxi in news[0].select('li'):

time=xiaoxi.contents[2]

for a in xiaoxi.select('a'):

path=a['href']

str=""

path=str+path

title=a['title']

f.write(title+' ')

f.write(path+' ')

f.write(time+' ')

f.write("\n\n")

for i in range(2,88) :

res = requests.get("不告訴你/zonghexw/index_%d.html"%(i))

res.encoding='gb2312'

soup =beautifulsoup(res.text,'html.parser')

news=soup.select('.newsgridlist')

for xiaoxi in news[0].select('li'):

time=xiaoxi.contents[2]

for a in xiaoxi.select('a'):

path=a['href']

str="不告訴你"

path=str+path

title=a['title']

f.write(title+' ')

f.write(path+' ')

f.write(time)

f.write("\n\n")

將每乙個檔案的內容存到本地

#首頁的詳細新聞

res = requests.get('不告訴你')

res.encoding='gb2312'

soup =beautifulsoup(res.text,'html.parser')

news=soup.select('.newsgridlist')

for xiaoxi in news[0].select('li'):

time=xiaoxi.contents[2]

for a in xiaoxi.select('a'):

path=a['href']

str="不告訴你"

path=str+path

res2 =requests.get(path)

res2.encoding='gb2312'

soup2=beautifulsoup(res2.text,'html.parser')

ntitle=soup2.select('.title')

ntitle1=ntitle[0].text

with open('d:/抓的資料/詳細新聞/'+ntitle1+'.txt','w',encoding='utf-8') as f1:

還可以爬一些使用者資訊之類的,儲存為csv檔案,用excel開啟處理。

感覺就是python太強大了

Python網路爬蟲心得(適合入門新手)

網路爬蟲,顧名思義,乙隻蟲,在網路上爬取資料。1 如何快速入門網路爬蟲?簡單描述 可以這麼想象,他就是我們的乙個分身,我們用 付給它我們想要杆什麼的指令,然後它就 活 了,於是他就開始逛逛 看看資料,或者是將我們需要的資料帶回來。3 如何實現?比較正規的 一般開發地都非常的規則,網頁的結構都相同,就...

Python爬蟲 編寫簡單爬蟲之新手入門

最近整理了一下python的基礎知識,大家一般對 爬蟲 這個詞,一聽就比較熟悉,都知道是爬一些 上的資料,然後做一些操作整理,得到人們想要的資料,但是怎麼寫乙個爬蟲程式 呢?相信很多人是不會的,今天寫乙個針對新手入門想要學習爬蟲的文章,希望對想要學習的你能有所幫助 廢話不多說,進入正文!1 首先 使...

python 爬蟲 慶餘年小說 詞云胡亂分析

進入正題,首先要搞到資源,我先去了搜尋了一番,找到個 落霞 一言不合就按下了f12,翻了下網頁原始碼,超級簡單。from bs4 import beautifulsoup from requests import session from re import sub,dotall sess sess...