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

1 Pyhton取得網頁的內容(也就是原始碼)(推薦學習:Python影片教學)
page = urllib2.urlopen(url) 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('http://movie.douban.com/top250?format=text')
contents = page.read()
#print(contents)
soup = BeautifulSoup(contents,"html.parser")
print("豆瓣电影TOP250" + "\n" +" 影片名 评分 评价人数 链接 ")
for tag in soup.find_all('div', 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('div',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相關技術文章,請造訪Python教學欄位進行學習!
以上是python 怎麼取得網頁內容的詳細內容。更多資訊請關注PHP中文網其他相關文章!