ただただソースを読んで、その内容をコンソールに表示
from selenium import webdriver from selenium.webdriver.chrome.service import Service service = Service("..\\path\\chromedriver.exe") driver = webdriver.Chrome(service=service) url = '対象URL' driver.get(url) page_source = driver.page_source print(page_source) driver.quit()
Chromeのログイン情報を保持しながらスクレイピングする
Seleniumを使用して開いたChromeのログイン情報を残す。
これで一回ログインした情報を残す。
from selenium import webdriver # 新しいChromeプロファイルを作成 options = webdriver.ChromeOptions() options.add_argument("--user-data-dir=C:/Users/YourUsername/AppData/Local/Google/Chrome/User Data/ProfileName") driver = webdriver.Chrome(options=options)
これにより、前回のセッションからログイン情報を引き継ぐことができます。
BeautifulSoupでCSSセレクタでfindする方法
BeautifulSoupのselectを使用する
beautiful_soup_objct.select('div#test div span.last a')
これならCSSをこねくり回す私でも大丈夫!
URLから最後のスラッシュ以降の値を取得する
rfindを使えばすぐに取得可能
str_objct[str_objct.rfind('[sepalater]') + 1:]
こんな感じ
last_url[last_url.rfind('/') + 1:])
BeautifulSoupでソースから特定のリンクだけ抽出してみた。
#find_all('[タグ]', class_="[クラス名]")でaタグ全体を抽出 for element in soup.find_all('a', class_="link"): #get関数の引数に「属性」を入れて内部を抜き出す。 url = element.get('href')
実際うまくいったコード
import requests from bs4 import BeautifulSoup url = 'リンク先' html = requests.get(url) soup = BeautifulSoup(html.content, 'html.parser') for element in soup.find_all('a', class_="link"): url = element.get('href') print(url)
Shopifyの商品詳細で値を取得する
■タイトルを取得する
{{ product.title }}
■説明を取得する
{{ product.description }}
■価格を取得する(ここは「product-price.liquid」を呼び出しているだけ。)
{% include 'product-price', variant: current_variant, show_vendor: section.settings.show_vendor %}
個人的には楽天の「PC商品説明文」のように縦長のスタイルにしたいので、
以下のコードを
<div class="product-single__description rte"> {{ product.description }} </div>
以下のDIVタグから出してあげて
<div class="product-template__container page-width" id="ProductSection-{{ section.id }}" data-section-id="{{ section.id }}" data-section-type="product" data-enable-history-state="true" data-ajax-enabled="{{ settings.enable_ajax }}" >
最後にCSSを当ててあげると完璧です。
これShopifyカスタマイズできたら、結構自由度高くなりますな。