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

極客小將

您現在的位置是:首頁 » python編程資訊

資訊內容

介紹python爬取網頁

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

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

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

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

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

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

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

如圖3SL少兒編程網-https://www.pxcodes.com



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

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

import urllib.requestfrom bs4 import BeautifulSoupimport xlwtimport redef main(): # 爬取網頁 baseurl = 'https://movie.douban.com/top250?start=' datalist = getData(baseurl) savepath = '豆瓣電影Top250.xls' # 保存數據 saveData(datalist,savepath) # askURL("https://movie.douban.com/top250?start=1")#影片詳情的規則findLink = re.compile(r'<a class="" href="(.*?)">') #創建從正則表達式,表示規則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>')#找到評價人數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忽視換行符

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

def getData(baseurl): datalist = [] for i in range(0, 10): url = baseurl + str(i*25) html = askURL(url) #保存獲取到的網頁源碼 #對網頁進行解析 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) #評價人數 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少兒編程網-https://www.pxcodes.com

#得到指定的一個url網頁信息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

第四部分:保存數據3SL少兒編程網-https://www.pxcodes.com

# 3:保存數據def saveData(datalist,savepath): book = xlwt.Workbook(encoding="utf-8", style_compression=0) sheet = book.add_sheet('豆瓣電影Top250', cell_overwrite_ok=True) col = ('電影詳情鏈接', '圖片鏈接', '影片中文名', '影片外國名', '評分', '評價數', '概況', '相關信息') 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少兒編程網-https://www.pxcodes.com

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

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

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

預約試聽課

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

主站蜘蛛池模板: 淄博市| 中西区| 佛坪县| 肥乡县| 兰西县| 平南县| 兴国县| 开远市| 太白县| 岑溪市| 榕江县| 莱西市| 巴彦淖尔市| 保靖县| 军事| 绥阳县| 北海市| 南投市| 呼玛县| 赣榆县| 张家界市| 安岳县| 芒康县| 漠河县| 宁化县| 湖州市| 阳泉市| 中西区| 马尔康县| 辽源市| 青海省| 长岛县| 邵阳市| 嵊州市| 阳曲县| 东台市| 武隆县| 东辽县| 海盐县| 香格里拉县| 准格尔旗|