從xml配置檔案中取配置的常用欄位存入快取,當檔案更新時同步更新檔案內容到快取中。
阿新 • • 發佈:2022-04-21
一、re
import requests import re url = "https://movie.douban.com/top250" headers = { "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"} # 請求頭 response = requests.get(url, headers=headers) text = response.text obj = re.compile(r"""<li>.*?<span class="title">(?P<name>.*?)</span>.*?<p class="">(?P<director>.*?) (?P<lead>.*?)...<br>(?P<time>.*?) / (?P<country>.*?) / (?P<type>.*?)\s\s\s.*?<span class="rating_num" property="v:average">(?P<score>.*?)</span>.*?<span>(?P<number>.*?)</span>.*?<span class="inq">(?P<quote>.*?)</span>""", re.S) result = obj.finditer(text) # 在text中查詢obj(是一個Pattern物件),返回一個迭代器 for i in result: print(i.group("name")) # group()用於分組 print(i.group("director").strip()) # 去掉空格 print(i.group("lead")) print(i.group("time").strip()) print(i.group("country")) print(i.group("type"))print(i.group("score")) print(i.group("number")) print(i.group("quote")) response.close()
結果為:
肖申克的救贖 導演: 弗蘭克·德拉邦特 Frank Darabont 主演: 蒂姆·羅賓斯 Tim Robbins / 1994 美國 犯罪 劇情 9.7 2613829人評價 希望讓人自由。 霸王別姬 導演: 陳凱歌 Kaige Chen 主演: 張國榮 Leslie Cheung / 張豐毅 Fengyi Zha 1993 中國大陸 中國香港 劇情 愛情 同性 9.6 1940678人評價 風華絕代。……
詳細介紹請看python正則表示式
二、BeautifulSoup
import requests from bs4 import BeautifulSoup url = "http://zhongdapeng.com/shucaijiage/831.html" response = requests.get(url) response.encoding = "utf-8" text = response.text # 先把頁面原始碼交給BeautifulSoup處理,生成bs物件 page = BeautifulSoup(text, "html.parser") # 指定為html解析器(parser) # 再從bs物件中查詢資料:1、find(只找一個) 2、find_all 這兩者後面都是(標籤,屬性=值),注意屬性可能為python的關鍵字,比如class,那麼就要寫成class_=值或者attrs={"class":值} table = page.find("table", attrs={"cellspacing": "0"}) trs = table.find_all("tr")[1:] # 去掉第0個 for tr in trs: tds = tr.find_all("td") print(tds[1].text, tds[2].text, tds[4].text, tds[5].text) # .text表示被標籤標記的內容 response.close()
結果為:
義大利 1.6-1.8 香菜 5.0-6.0 油麥菜 1.7-1.7 菠菜 3.0-3.5 上青 0.3-0.4 茼蒿 1.3-1.5 ……
三、xpath
import requests from lxml import etree url = "https://search.jd.com/Search?keyword=iphone&enc=utf-8&wq=iphone&pvid=eb21f8da6aad40ccb455dd20c5ce52d4" headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"} # 請求頭 response = requests.get(url, headers=headers) response.encoding = "utf-8" text = response.text treeoject = etree.HTML(text) # 解析html # 根據xpath來提取元素 lis = treeoject.xpath("""/html/body/div[5]/div[2]/div[2]/div[1]/div/div[2]/ul/li""") for li in lis: # /text()獲取標籤內的文字 price = li.xpath("""./div/div[3]/strong/i/text()""")[0] description = "iPhone".join(li.xpath("""./div/div[4]/a/em/text()""")).strip() # 去掉空格 merchandiser = li.xpath("""./div/div[7]/span/a/text()""")[0] print(price) print(description) print(merchandiser) print("") # 可以用屬性值索引:li[@class='…']/… # //li代表匹配所有的後代li節點,不論幾代 # ./代表從相對目錄開始查詢 # *代表萬用字元 # @用來提取屬性值:li/@class response.close()
結果為:
6098.00 Apple iPhone 13 (A2634) 128GB 星光色 支援移動聯通電信5G 雙卡雙待手機【快充套裝】 Apple產品京東自營旗艦店 3999.00 Apple iPhone 11 (A2223) 128GB 黑色 移動聯通電信4G手機 雙卡雙待 Apple產品京東自營旗艦店 5499.00 Apple iPhone 13 128GB 午夜色 支援移動聯通電信5G 雙卡雙待手機 廣州電信京東自營旗艦店 ……