前提・実現したいこと
PythonのGUIによるスクレイピング機能の実装を行いたいと思っております。
内容は同じディレクトリの中にあるテキストからURLを取得して情報をGUIに書き出す内容になっております。
dataの空にスクレイピングを吐き出すための値を書くなどしてトライしましたが実行できませんでした。
実際にCVSなどに書き出すときなどは問題がありませんが、GUIに表記されている内容を一覧で書き出すにはどのようにコードを追加したら良いでしょうか?
該当のソースコード
スクレイピングのコード
from bs4 import BeautifulSoup import urllib.request as req import csv f = open('url.txt') lines2 = f.readlines() f.close() pa = [] # 取得 for line in lines2: url = "xxxxxxxxxxxxxx" + line res = req.urlopen(url) soup = BeautifulSoup(res, 'html.parser') title1 = soup.select('#item_title') for i in title1: pt = i.get_text().replace('\n', '').replace('\r', '') p_list = soup.select('#price') for i in p_list: pl = i.get_text().replace('\n', '').replace('\r', '') pa += [pt, pl] # CSV 書き出し with open('price.csv', 'w', encoding='utf-8') as _file: writer = csv.writer(_file, lineterminator='\n') writer.writerow(pa)
PyQtのコード
# /user/bin/pyrhon3 # -*- coding: utf-8 -*- import sys from PyQt5.QtWidgets import (QApplication, QWidget, QPushButton, QTableWidgetItem, QTableWidget) import logging data = [] class tableTest01(QTableWidget): """ テーブルを形成する関数 """ def __init__(self, data, *args): QTableWidget.__init__(self, *args) self.data = data self.setdata() self.resizeColumnsToContents() self.resizeRowsToContents() def setdata(self): #ヘッダーを作成 horHeaders = ['対象サイトのURL', '商品名', '現在価格', '希望価格'] self.setHorizontalHeaderLabels(horHeaders) #テーブルにデータをセット for n in range(len(self.data)): print("n=%d" % n) for m in range(len(self.data[n])): print("m=%d" % m) newitem = QTableWidgetItem(data[n][m]) self.setItem(m, n, newitem) if __name__ == '__main__': app = QApplication(sys.argv) """ 大枠 """ window = QWidget() window.resize(500, 500) window.setStyleSheet('background-color: #F2F2F2;') """ ボタン """ button = QPushButton('スクレイピングを開始', window) # button button.resize(255, 72) button.setStyleSheet("background-color:#F2FBEF; margin-top:25px; margin-left:25px; font-wight:bold; font-family:'Meiryo'; letter-spacing: 10px;") table = tableTest01(data, 3, 4) table.show() window.show() sys.exit(app.exec_())
あなたの回答
tips
プレビュー