LoginSignup
0
0

More than 5 years have passed since last update.

【Python】Linkを取得したいときのスクリプト-初歩

Posted at

はじめに

昔、自然言語処理領域でのデータ集めのためにスクレイピングのスクリプトを作ってました。
昔のログをあさっていたら、その際のスニペットが発掘されたので、このまま闇に葬るのもあれなので
Qiita上にアップロードします。

かなり初歩的なリンクのみを抜き出すスクリプトで、phantomJSを使用しているのでSPAページからリンクを抜き出したかったのかと思います(すっとぼけ)。

きっとurl が正しく指定されているならば、きっと動くはず。。。

スクリプト

get_links_by_phantomjs.py
import functools as ft
import lxml.html
from selenium import webdriver
import re

def get_link():
    driver = webdriver.PhantomJS()
    for page in range(1,12+1):
        url = f"http://ssmania.info/category/%E3%83%87%E3%83%AC%E3%83%9E%E3%82%B9?page={page}"
        driver.get(url)
        root = lxml.html.fromstring(driver.page_source)

        link_urls = [link_element.get("href") for link_element in root.cssselect('a.articlelink')]
        print(link_urls)
        yield link_urls

get_link_gen = get_link()

with open("linkes.dat", "w") as f:
    for links in get_link_gen:
        for link in links:
            print(link)
            f.write(link)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0