国产日韩欧美一区二区三区综合,日本黄色免费在线,国产精品麻豆欧美日韩ww,色综合狠狠操

極客小將

您現(xiàn)在的位置是:首頁 » python編程資訊

資訊內容

介紹python爬取網(wǎng)頁

極客小將2021-03-10-
簡介之前在網(wǎng)上也寫了不少關于爬蟲爬取網(wǎng)頁的代碼,最近還是想把寫的爬蟲記錄一下,方便大家使用吧!代碼一共分為4部分:第一部分:找一個網(wǎng)站。我這里還是找了一個比較簡單的網(wǎng)站,就是大家都知道的https://movie.douban.com/top250?start=大家可以登錄里面看一下。這里大家可能會有一
3SL少兒編程網(wǎng)-https://www.pxcodes.com

之前在網(wǎng)上也寫了不少關于爬蟲爬取網(wǎng)頁的代碼,**近還是想把寫的爬蟲記錄一下,方便大家使用吧!3SL少兒編程網(wǎng)-https://www.pxcodes.com

代碼一共分為4部分:3SL少兒編程網(wǎng)-https://www.pxcodes.com

第一部分:找一個網(wǎng)站。3SL少兒編程網(wǎng)-https://www.pxcodes.com

我這里還是找了一個比較簡單的網(wǎng)站,就是大家都知道的https://movie.douban.com/top250?start= 大家可以登錄里面看一下。

這里大家可能會有一些庫沒有進行安裝,先上圖讓大家安裝完爬取網(wǎng)頁所需要的庫,其中我本次用到的庫有:bs4,urllib,xlwt, re。3SL少兒編程網(wǎng)-https://www.pxcodes.com

(免費學習推薦:python視頻教程)3SL少兒編程網(wǎng)-https://www.pxcodes.com

如圖3SL少兒編程網(wǎng)-https://www.pxcodes.com



這里選擇file-setting-Project-然后選擇左下角的加號,自行去安裝自己所需要的文件就可以了。3SL少兒編程網(wǎng)-https://www.pxcodes.com

下面的代碼是爬取網(wǎng)頁的源代碼:3SL少兒編程網(wǎng)-https://www.pxcodes.com

import urllib.requestfrom bs4 import BeautifulSoupimport xlwtimport redef main(): # 爬取網(wǎng)頁 baseurl = 'https://movie.douban.com/top250?start=' datalist = getData(baseurl) savepath = '豆瓣電影Top250.xls' # 保存數(shù)據(jù) saveData(datalist,savepath) # askURL("https://movie.douban.com/top250?start=1")#影片詳情的規(guī)則findLink = re.compile(r'<a class="" href="(.*?)">') #創(chuàng)建從正則表達式,表示規(guī)則findImgSrc = re.compile(r'<img.*src="(.*?)"', re.S) #讓換行符匹配到字符中#影片的片名finTitle = re.compile(r'<span class="title">(.*)</span>')#影片的評分findReating = re.compile(r'<span class="rating_num" property="v:average">(.*)</span>')#找到評價人數(shù)findJudge = re.compile(r'<span>(d*)人評價</span>')#找到概況findInq = re.compile(r'<span class="inq">(.*)</span>')#找到影片的相關內容findBb = re.compile(r'<p class="">(.*?)</p>', re.S)#re.S忽視換行符

第二部分:爬取網(wǎng)頁。3SL少兒編程網(wǎng)-https://www.pxcodes.com

def getData(baseurl): datalist = [] for i in range(0, 10): url = baseurl + str(i*25) html = askURL(url) #保存獲取到的網(wǎng)頁源碼 #對網(wǎng)頁進行解析 soup = BeautifulSoup(html, 'html.parser') for item in soup.find_all('p', class_="item"): #查找符合要求的字符串 形成列表 #print(item) #測試查看電影信息 data = [] item = str(item) link = re.findall(findLink, item)[0] #re庫用來查找指定的字符串 data.append(link) imgSrc = re.findall(findImgSrc, item)[0] data.append(imgSrc) #添加圖片 titles = re.findall(finTitle, item) # if (len(titles) == 2): ctitle = titles[0] #添加中文名 data.append(ctitle) otitle = titles[1].replace("/", "") #replace("/", "")去掉無關的符號 data.append(otitle) #添加英文名 else: data.append(titles[0]) data.append(' ')#外國名字留空 rating = re.findall(findReating, item)[0] #添加評分 data.append(rating) judgeNum = re.findall(findJudge,item) #評價人數(shù) data.append(judgeNum) inq = re.findall(findInq, item) #添加概述 if len(inq) != 0: inq = inq[0].replace(".", "") #去掉句號 data.append(inq) else: data.append(" ") #留空 bd = re.findall(findBb,item)[0] bd = re.sub('<br(s+)?/>(s+)?',' ', bd) #去掉br 后面這個bd表示對bd進行操作 bd = re.sub('/', ' ', bd) #替換/ data.append(bd.strip()) #去掉前后的空格strip() datalist.append(data) #把處理好的一部電影放入datalist當中 return datalist

第三部分:得到一個指定的url信息。3SL少兒編程網(wǎng)-https://www.pxcodes.com

#得到指定的一個url網(wǎng)頁信息def askURL(url): head = { "User-Agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Mobile Safari/537.36"} request = urllib.request.Request(url,headers=head) # get請求不需要其他的的,而post請求需要 一個method方法 html = "" try: response = urllib.request.urlopen(request) html = response.read().decode('utf-8') # print(html) except Exception as e: if hasattr(e,'code'): print(e.code) if hasattr(e,'reason'): print(e.reason) return html

第四部分:保存數(shù)據(jù)3SL少兒編程網(wǎng)-https://www.pxcodes.com

# 3:保存數(shù)據(jù)def saveData(datalist,savepath): book = xlwt.Workbook(encoding="utf-8", style_compression=0) sheet = book.add_sheet('豆瓣電影Top250', cell_overwrite_ok=True) col = ('電影詳情鏈接', '圖片鏈接', '影片中文名', '影片外國名', '評分', '評價數(shù)', '概況', '相關信息') for i in range(0,8): sheet.write(0,i,col[i]) #列名 for i in range(0,250): print("第%d條"%i) data = datalist[i] for j in range(0,8): sheet.write(i+1,j,data[j]) book.save(savepath) #保存

這里大家看一下代碼,關于代碼的標注我寫的還是挺清楚的。3SL少兒編程網(wǎng)-https://www.pxcodes.com

其中關于學習這個爬蟲,還需要學習一些基本的正則表達式,當然python基本的語法是不可少的希望對大家有幫助吧。3SL少兒編程網(wǎng)-https://www.pxcodes.com

相關免費學習推薦:python教程(視頻)

以上就是介紹python爬取網(wǎng)頁的詳細內容,更多請關注少兒編程網(wǎng)其它相關文章!3SL少兒編程網(wǎng)-https://www.pxcodes.com

預約試聽課

已有385人預約都是免費的,你也試試吧...

主站蜘蛛池模板: 铜陵市| 轮台县| 比如县| 克拉玛依市| 广东省| 山西省| 农安县| 涿鹿县| 涟水县| 施秉县| 浑源县| 仪陇县| 大英县| 永昌县| 新蔡县| 泰安市| 大石桥市| 清徐县| 同江市| 河北区| 泗洪县| 浪卡子县| 巫溪县| 同心县| 肥东县| 阳信县| 渭南市| 永德县| 巴林右旗| 保亭| 明星| 大城县| 乌恰县| 洞口县| 闻喜县| 普定县| 霍州市| 文安县| 枞阳县| 张家口市| 安陆市|