python爬蟲 爬取靜態網頁

2021-09-11 03:21:01 字數 2159 閱讀 5580

爬蟲新手剛入門,萌新練手交流作

import requests

import bs4

from bs4 import beautifulsoup

# 偽裝瀏覽器,獲取源**

def gethtml(url):

headers= # 偽裝瀏覽器

response = requests.get(url,headers=headers) # 使用request的宣告

if response.status_code == 200:

return response.text # 如果響應正常,返回網頁源**

return none

# 獲取頁面上所有**

def get_chinese_novel(url):

html = gethtml(url)

soup = beautifulsoup(html, 'lxml') #使用beautifulsoup的lxml庫

novels_dir = # 定義列表

for all_li in soup.find_all('li', class_='media'): #尋找所有標籤下的為media的節點的內容

if isinstance(all_li, bs4.element.tag): #判斷返回的all_al 是否為bs4.element.tag型別

for all_novels in all_li.find_all('a', class_='text-white'):

novels_info = {}

novels_info['name'] = (all_novels.get_text().strip('\n')) # 將節點內容的文字放入字典

novels_info['url'] = '' + all_novels.get('href') #將與節點中的裡的鏈結合起來放入字典

return novels_dir #返回列表

# 獲取具體的**的目錄,只是url變成了具體哪本**的url

def get_novel_dir(url):

html = gethtml(url)

soup = beautifulsoup(html, 'lxml')

books_dir =

books_info = {}

for all_title in soup.find('div', class_='panel-group').children:

if isinstance(all_title, bs4.element.tag):

books_info = {}

for all_a in all_title.find_all('li', class_='chapter-item'):

books_info['name'] = (all_a.get_text())

books_info['url'] = '' + all_a.a.get('href')

return books_dir

#將 get_chinese_novel()獲得的列表轉化為字典

def novels_dict(url):

dict = {}

list_novels = get_chinese_novel(url)

for i in range(len(list_novels) - 1):

if i % 2 == 0:

dict[list_novels[i]] = list_novels[i + 1] # 將列表兩兩配對成字典,便於根據書名查詢鏈結

else:

continue

return dict

url = '/language/chinese'

novel_name = input("請輸入書名:")

Python 爬蟲爬取網頁

工具 python 2.7 import urllib import urllib2 defgetpage url 爬去網頁的方法 request urllib.request url 訪問網頁 reponse urllib2.urlopen request 返回網頁 return response...

10 30簡單爬蟲 靜態網頁爬取

爬取讀者文章 網頁資料分為 動態網頁資料和靜態網頁資料,兩種網頁資料的爬取是不一樣的 爬蟲思路 當前頁面的網頁源 其中獲取需要的資訊 import requests requests 是乙個 版的瀏覽器 from bs4 import beautifulsoup 資料的提取 解析 三大解析工具之一 ...

Python爬取靜態網頁操作

靜態網頁一般指純粹的html格式的網頁,對於爬蟲來說,靜態網頁的資料都比較容易獲取,利用好requests庫就能輕鬆傳送http請求,獲取到網頁的資料。requests庫可以幫助我們獲取到響應內容,再通過一些引數來滿足我們的需求,它的安裝也十分簡單,對於windows使用者來說,在已經裝好pytho...