python 獲取網頁內容 python

2021-10-16 23:32:09 字數 1200 閱讀 2550

詳細內容

python用做資料處理還是相當不錯的,如果你想要做爬蟲,python是很好的選擇,它有很多已經寫好的類包,只要呼叫,即可完成很多複雜的功能。

contents = page.read()

#獲得了整個網頁的內容也就是源**

print(contents)

url代表**,contents代表**所對應的源**,urllib2是需要用到的包,以上三句**就能獲得網頁的整個源**

2 獲取網頁中想要的內容(先要獲得網頁源**,再分析網頁源**,找所對應的標籤,然後提取出標籤中的內容)

以豆瓣電影排名為例子

現在我需要獲得當前頁面的所有電影的名字,評分,評價人數,鏈結#coding:utf-8

@author: jsjxy

import urllib2

import re

from bs4 import beautifulsoup

from distutils.filelist import findall

page = urllib2.urlopen('')

contents = page.read()

#print(contents)

soup = beautifulsoup(contents,"html.parser")

print("豆瓣電影top250" + "\n" +" 影片名 評分 評價人數 鏈結 ")

for tag in soup.find_all('span', class_='info'):

# print tag

m_name = tag.find('span', class_='title').get_text()

m_rating_score = float(tag.find('span',class_='rating_num').get_text())

m_people = tag.find('span',class_="star")

m_span = m_people.findall('span')

m_peoplecount = m_span[3].contents[0]

m_url=tag.find('a').get('href')

print( m_name+" " + str(m_rating_score) + " " + m_peoplecount + " " + m_url )

控制台輸出,你也可以寫入檔案中

python獲取網頁內容

需要用到bs4套件來獲取網頁中的文字,如果沒有新增此套件,可以根據以下操作來新增 win r,輸入cmd,輸入pip install beautifulsoup4,因為我已經安裝過了所以出現以下內容 安裝完成後即可開始編寫 如下 1 import requests2 匯入bs4套件 3from bs...

Python 獲取 html 網頁內容

一篇基礎文章,不講爬蟲。單純的獲取標籤元素的值 操作網頁。用到了 selenium 包。這個包需要給瀏覽器安裝驅動,不同的瀏覽器需要的驅動不同。環境搭建參考 需要注意,windows版本的驅動檔案.exe需要放在python.exe所在的目錄下,環境變數才能生效 別問我為什麼,我也不知道 打 狐瀏覽...

python開啟網頁獲取網頁內容方法總結

在學習python爬蟲的過程中,總會遇到要獲取網頁內容的時候,下面就對如何獲取網頁內容進行總結。方法一 import urllib url 這裡是需要獲取的網頁 content urllib.open url read 使用urllib模組獲取網頁內容 print content 輸出網頁的內容 功...