requests + re 爬去網站圖書資訊(Python)
import requests
import re, json
if __name__ == '__main__':
content = requests.get('https://book.douban.com/').text
reg_base = '<ul.*?list-col list-col5 list-express slide-item">(.*?)</ul>'
base_pattern = re.compile(reg_base, re.S)
base_html = re.findall(base_pattern, content)
href = '<li.*?cover.*?href="(.*?)".*?'
title = '<div.*?title.*?title.*?>(.*?)</a>.*?'
author = '<div.*?more-meta.*?author.*?>(.*?)</span>.*?</li>'
regex = href + title + author
pattern = re.compile(regex, re.S)
results = []
# 匹配子標籤中的圖書資訊
for html in base_html:
results += re.findall(pattern, html)
with open('touban.txt', 'w', encoding='utf-8') as f:
for result in results:
d = {
'href': result[0].strip(),
'title': result[1].strip(),
'author': result[2].replace(' ', '').strip()
}
f.write(json.dumps(d, ensure_ascii=False) + '\n')
{"author": "[法] 米歇爾·普西", "href": "https://book.douban.com/subject/30180673/?icn=index-editionrecommend", "title": "她不是我媽媽"} {"author": "[意]馬西米利亞諾·威爾吉利奧", "href": "https://book.douban.com/subject/30180821/?icn=index-editionrecommend", "title": "那不勒斯的螢火"} {"author": "於蕾,呂逸濤", "href": "https://book.douban.com/subject/30206904/?icn=index-editionrecommend", "title": "國家寶藏"} {"author": "張立民", "href": "https://book.douban.com/subject/30235899/?icn=index-editionrecommend", "title": "最後一公里的哲學:電商物流全鏈條運營管理"} {"author": "【英】詹姆斯•霍尼伯內(James Honeyborne)/【英】馬克•布朗羅(Mark Brownlow)", "href": "https://book.douban.com/subject/30183403/?icn=index-editionrecommend", "title": "BBC全新4K海洋百科:藍色星球II"} {"author": "[葡] 若澤·薩拉馬戈", "href": "https://book.douban.com/subject/27598520/?icn=index-latestbook-subject", "title": "裡卡爾多·雷耶斯離世那年"} {"author": "[美] 史蒂芬·平克", "href": "https://book.douban.com/subject/30186025/?icn=index-latestbook-subject", "title": "風格感覺"} {"author": "趙壘", "href": "https://book.douban.com/subject/30204837/?icn=index-latestbook-subject", "title": "傀儡城之荊軻刺秦"} {"author": "梅貽琦/黃延復/王小寧", "href": "https://book.douban.com/subject/30197575/?icn=index-latestbook-subject", "title": "梅貽琦西南聯大日記"} {"author": "[日] 永井荷風", "href": "https://book.douban.com/subject/30171301/?icn=index-latestbook-subject", "title": "濹東綺譚"} {"author": "[波蘭] 安傑伊·瓦伊達/Andrzej Wajda", "href": "https://book.douban.com/subject/30211002/?icn=index-latestbook-subject", "title": "我們一起拍片!"} {"author": "[德] 弗蘭克·施茨廷", "href": "https://book.douban.com/subject/27604676/?icn=index-latestbook-subject", "title": "群"} {"author": "[美] 克麗絲特爾·潘恩/Crystal Paine", "href": "https://book.douban.com/subject/30206819/?icn=index-latestbook-subject", "title": "會賺錢的媽媽"} {"author": "[日] 石田衣良", "href": "https://book.douban.com/subject/27622428/?icn=index-latestbook-subject", "title": "美麗的孩子"} {"author": "楊時暘", "href": "https://book.douban.com/subject/30218577/?icn=index-latestbook-subject", "title": "孤獨的影獵人"} {"author": "[德]沃爾夫岡·赫倫多夫", "href": "https://book.douban.com/subject/27598521/?icn=index-latestbook-subject", "title": "小心,沙漠有人"} {"author": "[英] 珍妮特·溫特森", "href": "https://book.douban.com/subject/27663541/?icn=index-latestbook-subject", "title": "我要快樂,不必正常"} {"author": "朱一葉", "href": "https://book.douban.com/subject/30198364/?icn=index-latestbook-subject", "title": "死於象蹄"} {"author": "[荷] 伊恩·布魯瑪", "href": "https://book.douban.com/subject/27662697/?icn=index-latestbook-subject", "title": "日本之鏡"} {"author": "[美] 威廉·莫爾頓·馬斯頓", "href": "https://book.douban.com/subject/30210732/?icn=index-latestbook-subject", "title": "神奇女俠"} {"author": "[美] 特德·焦亞", "href": "https://book.douban.com/subject/30203912/?icn=index-latestbook-subject", "title": "如何聽爵士"} {"author": "鄧安慶", "href": "https://book.douban.com/subject/30221630/?icn=index-latestbook-subject", "title": "紙上王國"} {"author": "朱偉", "href": "https://book.douban.com/subject/30205589/?icn=index-latestbook-subject", "title": "重讀八十年代"} {"author": "鄧安慶", "href": "https://book.douban.com/subject/30190319/?icn=index-latestbook-subject", "title": "望花"} {"author": "[美]沃爾特·李普曼", "href": "https://book.douban.com/subject/27662713/?icn=index-latestbook-subject", "title": "輿論"} {"author": "[英] P•D•詹姆斯", "href": "https://book.douban.com/subject/27111572/?icn=index-latestbook-subject", "title": "人類之子"} {"author": "駱儀", "href": "https://book.douban.com/subject/30198500/?icn=index-latestbook-subject", "title": "京都好物"} {"author": "(美) 比爾·克林頓 (Bill Clinton)/[美] 詹姆斯·帕特森", "href": "https://book.douban.com/subject/30218923/?icn=index-latestbook-subject", "title": "失蹤的總統"} {"author": "劉冰/林秦文/李敏", "href": "https://book.douban.com/subject/30203973/?icn=index-latestbook-subject", "title": "中國常見植物野外識別手冊(北京冊)"} {"author": "冶文彪", "href": "https://book.douban.com/subject/30205286/?icn=index-latestbook-subject", "title": "清明上河圖密碼 5"} {"author": "[英] 勞拉·卡琳/Laura Carlin", "href": "https://book.douban.com/subject/30181220/?icn=index-latestbook-subject", "title": "創造自己的世界"} {"author": "郭強生", "href": "https://book.douban.com/subject/30217599/?icn=index-latestbook-subject", "title": "斷代"} {"author": "史傑鵬", "href": "https://book.douban.com/subject/30183948/?icn=index-latestbook-subject", "title": "悠悠我心"} {"author": "[俄] 柳德米拉·烏利茨卡婭", "href": "https://book.douban.com/subject/30205823/?icn=index-latestbook-subject", "title": "庫科茨基醫生的病案"} {"author": "[美] 蘭德爾·柯林斯", "href": "https://book.douban.com/subject/30143236/?icn=index-latestbook-subject", "title": "文憑社會"} {"author": "[法] 讓-皮埃爾·吉布拉", "href": "https://book.douban.com/subject/30205166/?icn=index-latestbook-subject", "title": "愛的緩刑"} {"author": "[美]麗貝卡·特雷斯特", "href": "https://book.douban.com/subject/30128172/?icn=index-latestbook-subject", "title": "單身女性的時代"} {"author": "[俄] 弗拉基米爾·索羅金", "href": "https://book.douban.com/subject/27200259/?icn=index-latestbook-subject", "title": "碲釘國"} {"author": "蘇精", "href": "https://book.douban.com/subject/30218894/?icn=index-latestbook-subject", "title": "鑄以代刻"} {"author": "[英] 石黑一雄", "href": "https://book.douban.com/subject/30181685/?icn=index-latestbook-subject", "title": "莫失莫忘"}
相關推薦
requests + re 爬去網站圖書資訊(Python)
# -*- coding: utf-8 -*-import requestsimport re, jsonif __name__ == '__main__': content = requests.get('https://book.douban.com/').text
Python3爬蟲小程式——爬取各類天氣資訊(3)
經過前面靜態頁面的爬取已經收集到很多資訊了。最近在看怎麼從動態頁面上爬取資訊,主要用到的還是selenium+phantomJS工具(如何安裝?點選這裡檢視),雖然存在一些缺點(效率不是很高),但是還算不錯。 於是乎,前面從天氣網(http://www.tianqi.com
移動端爬蟲--專案實踐loach--爬去抖音資料(四)
文集 移動端爬蟲 原始碼 loach loach是一個移動端爬蟲,針對現下很火的短視訊app—抖音 支援多個android裝置並行自動化 支援任意android裝
雪球網爬取上市公司資訊(一):爬取上市公司代號
條件:有一批5g相關公司,只知道公司名字或是簡稱,不知道公司是否上市以及股票程式碼,需要爬取公司資訊。 網站:雪球網 思路:上傳關鍵字,爬取搜尋結果網頁,將有結果的公司資訊抓取下來並存入資料庫 1、在雪球網輸入公司名搜尋,發現返回3個結果,其中search.json?c
Django框架電商網站開發流程(Python)
一、配置 1 建立專案和應用 配置專案setting.py(應用,templates路徑,資料庫,STATICFILES_DIRS) 配置專案urls.py,namespace起名最好和應用名一樣,不容易混淆 2 配置應用 在每個
實戰--Scrapy框架爬去網站資訊
Scrapy的框架圖 一、使用Strapy抓取網站一共需要四個步驟: (1)建立一個Scrapy專案; (2)定義Item容器; Item是儲存爬取到的資料的容器,其使用方法和python字典類似,並且提供了額外保護機制來i避免拼寫錯誤導致的未定義欄位。
scrapy爬取愛上租網站的房源資訊(一)
爬取的頁面如下:愛上租的租房頁面 需要爬取該頁面下所有房間的基本資訊 scrapy框架的安裝和使用教程參考以下連結 http://www.scrapyd.cn/doc/178.html 首先在spiders目錄下新建一個house_spider.py,將上面爬
python 爬蟲 requests+BeautifulSoup 爬取巨潮資訊公司概況代碼實例
pan 字符 selenium 5.0 target 自我 color list tails 第一次寫一個算是比較完整的爬蟲,自我感覺極差啊,代碼low,效率差,也沒有保存到本地文件或者數據庫,強行使用了一波多線程導致數據順序發生了變化。。。 貼在這裏,引以為戒吧。 #
Python爬蟲:爬取網站電影資訊
以爬取電影天堂喜劇片前5頁資訊為例,程式碼如下: 1 # coding:UTF-8 2 3 import requests 4 import re 5 6 def mov(): 7 headers={'User-Agent':'Mozilla/5.0 (Windo
python爬蟲爬取非同步載入網頁資訊(python抓取網頁中無法通過網頁標籤屬性抓取的內容)
1.問題描述 最近由於學習內容的要求,需要從網頁上抓取一些資料來做分析報告,在看了python爬蟲的一些基礎知識之後就直接上手去網站上爬資料了。作為新手踩坑是無法避免,最近就遇到了一個比較難的問題: 一般情況下,要抓去網頁上某個標籤上的內容,在通過urllib下
Scrapy爬取前程無憂(51job)相關職位資訊
Scrapy爬取前程無憂(51job)python職位資訊 開始是想做資料分析的,上網上找教程,看到相關部落格我就跟著做,但是沒資料就只能開始自己爬唄。順便給51job的工作人員提提建議,我爬的時候Scrapy訪問量開到128,relay僅有兩秒,還以為會封ip。沒想到只是改請求頭就
Python(16)_爬去百度圖片(urlopen和urlretrieve)
pen mage com span style col with jpg bad import urllib.request image_url = ‘http://img18.3lian.com/d/file/201709/21/f498e01633b5b704ebf
requests+beautifulsoup爬取豆瓣圖書
使用Xpath和BeautifulSoup來解析網頁可以說真的很簡便。 import requests from bs4 import BeautifulSoup from random import choice url = 'https://book.douban.com/tag/%E7%BC%96%
(python)如何利用python深入爬取自己想要的資料資訊
一、問題說明 由於老師佈置了一個任務,需要對一個網站的城市做一些統計,並提取出這個網站上的城市的經緯度資訊然後繪製在百度地圖上。如果是一些數量不多的城市那也到好辦,但是如果對於這種存在幾百上千的城市,而且這些城市的經緯度資訊在第三級網站上才能找到,這樣一來,如果人工去完成會非常枯燥而
【圖文詳解】scrapy爬蟲與動態頁面——爬取拉勾網職位資訊(1)
5-14更新 注意:目前拉勾網換了json結構,之前是content - result 現在改成了content- positionResult - result,所以大家寫程式碼的時候要特別注意加上
爬蟲requests庫簡單抓取頁面資訊功能實現(Python)
import requests import re, json,time,random from requests import RequestException UserAgentList = [ "Mozilla/5.0 (Windows NT 6.1; WO
python 爬蟲爬取所有上市公司公告資訊(一)
。,。前面我們已經瞭解了python中多執行緒,selenium,requests等爬蟲基本操作的知識,現在我們準備編寫一個規模較大的爬蟲,目的是爬取所有上市公司2015年至今的公告資訊。 相較於前面幾個簡單的爬蟲功能程式碼,公告資訊爬蟲需要考慮更多的問題,現在可以預見到的
python3 爬蟲—爬取天氣預報多個城市七天資訊(三)
一、內容: 利用BeautifulSoup抓取中國天氣網各個城市7天的 時間 天氣狀態 最高溫 最低溫 的相關資訊,並記錄儲存在本地csv表格檔案中。 爬取的頁面截圖: html獲取資訊截圖: 二、原理: 1.利用requests獲取請求
爬蟲基本原理介紹和初步實現(以抓取噹噹網圖書資訊為例)
本文程式碼等僅作學習記錄使用 一、爬蟲原理 網路爬蟲指按照一定的規則(模擬人工登入網頁的方式),自動抓取網路上的程式。簡單的說,就是講你上網所看到頁面上的內容獲取下來,並進行儲存。網路爬蟲的爬行策略分為深度優先和廣度優先。 (1)、深度優先 深度
python 爬蟲爬取所有上市公司公告資訊(五)
。,。現在我們進過for迴圈,已經獲取到了每一個股票程式碼在2015年至今所有的公告內容連線和公告日期,且是以(日期,公告內容url)元組的形式加入到了爬取佇列中, 在最內層迴圈結束後,我們編寫程式實現多執行緒和儲存公告內容的功能。 公告最終在對應的json檔案裡是以鍵值對