質問編集履歴

4

コードを実行するとエラーが出てきました。修正したのでこのコードでしたらきちんと実行されます。

2020/07/28 09:37

投稿

konnoeric
konnoeric

スコア2

test CHANGED
File without changes
test CHANGED
@@ -34,7 +34,7 @@
34
34
 
35
35
  ### 該当のソースコード
36
36
 
37
- import requests
37
+ mport requests
38
38
 
39
39
  from bs4 import BeautifulSoup
40
40
 
@@ -56,7 +56,7 @@
56
56
 
57
57
  data_newfile = fd_newfile.readlines()
58
58
 
59
- newfile_list = data_newfile
59
+ newfile_list = data_newfile
60
60
 
61
61
  fd_newfile.close()
62
62
 
@@ -138,6 +138,8 @@
138
138
 
139
139
 
140
140
 
141
+
142
+
141
143
  ```ここに言語名を入力
142
144
 
143
145
  Pyrhon3

3

コードをそのまま加筆なしで上げました。

2020/07/28 09:37

投稿

konnoeric
konnoeric

スコア2

test CHANGED
File without changes
test CHANGED
@@ -38,6 +38,10 @@
38
38
 
39
39
  from bs4 import BeautifulSoup
40
40
 
41
+ import difflib as diff
42
+
43
+ import os
44
+
41
45
 
42
46
 
43
47
  newfile_list = 'a'
@@ -46,17 +50,17 @@
46
50
 
47
51
 
48
52
 
49
- #ファイルに書いてある内容をリスト型に格納する関数
50
-
51
53
  def readlines_newfile():
52
54
 
53
55
  fd_newfile = open('newfile.txt','r')
54
56
 
55
57
  data_newfile = fd_newfile.readlines()
56
58
 
57
- newfile_list = data_newfile
59
+ newfile_list = data_newfile
58
60
 
59
61
  fd_newfile.close()
62
+
63
+ print(data_newfile)
60
64
 
61
65
 
62
66
 
@@ -70,15 +74,29 @@
70
74
 
71
75
  fd_benzai.close()
72
76
 
73
-
74
-
75
- #requestsとbs4を用いてサイト記事から記事のテキストをファイル(newfile.txt)に書き込む(サイトURLは省略しています。)
76
-
77
- r = requests.get(link)
77
+ print(data_benzai)
78
78
 
79
79
 
80
80
 
81
+ r1 = requests.get("https://venzaiten.com/category/%e6%97%a5%e7%b5%8c225%e5%85%88%e7%89%a9/livetrade")
82
+
83
+
84
+
85
+ soup1 = BeautifulSoup(r1.text, "html.parser")
86
+
87
+ soup2 = soup1.find("li","post_type1 post_num1 clearfix" )
88
+
89
+ soup3 = soup2.find("h4", "title")
90
+
91
+ soup4 = soup3.find("a")
92
+
93
+ link = soup4.get("href")
94
+
95
+
96
+
97
+ r2 = requests.get(link)
98
+
81
- soupFinal = BeautifulSoup(r.text, "html.parser")
99
+ soupFinal = BeautifulSoup(r2.text, "html.parser")
82
100
 
83
101
  soupRealTradeCollum = soupFinal.find("div", "post_content clearfix").text
84
102
 
@@ -94,7 +112,9 @@
94
112
 
95
113
 
96
114
 
97
- #関数を実行
115
+ Entry = open("entry.txt", "w+")
116
+
117
+
98
118
 
99
119
  readlines_newfile()
100
120
 
@@ -102,9 +122,19 @@
102
122
 
103
123
 
104
124
 
125
+ print(benzai_list)
126
+
105
- #関数で作成したリストを比較(同じ内容のリストでもFalseと出てしまう)
127
+ print(newfile_list)
128
+
129
+
106
130
 
107
131
  print(benzai_list == newfile_list)
132
+
133
+
134
+
135
+ print(len(benzai_list))
136
+
137
+ print(len(newfile_list))
108
138
 
109
139
 
110
140
 

2

質問とは関係ないコードを省略しました。

2020/07/28 09:31

投稿

konnoeric
konnoeric

スコア2

test CHANGED
File without changes
test CHANGED
@@ -54,6 +54,8 @@
54
54
 
55
55
  data_newfile = fd_newfile.readlines()
56
56
 
57
+ newfile_list = data_newfile
58
+
57
59
  fd_newfile.close()
58
60
 
59
61
 
@@ -63,6 +65,8 @@
63
65
  fd_benzai = open('benzai.txt', 'r')
64
66
 
65
67
  data_benzai = fd_benzai.readlines()
68
+
69
+ benzai_list = data_benzai
66
70
 
67
71
  fd_benzai.close()
68
72
 

1

質問とは無関係なコードがありましたので省略しました。

2020/07/28 09:10

投稿

konnoeric
konnoeric

スコア2

test CHANGED
File without changes
test CHANGED
@@ -70,27 +70,11 @@
70
70
 
71
71
  #requestsとbs4を用いてサイト記事から記事のテキストをファイル(newfile.txt)に書き込む(サイトURLは省略しています。)
72
72
 
73
- r1 = requests.get("https:.com")
73
+ r = requests.get(link)
74
74
 
75
75
 
76
76
 
77
- soup1 = BeautifulSoup(r1.text, "html.parser")
78
-
79
- soup2 = soup1.find("li","post_type1 post_num1 clearfix" )
80
-
81
- soup3 = soup2.find("h4", "title")
82
-
83
- soup4 = soup3.find("a")
84
-
85
- link = soup4.get("href")
86
-
87
-
88
-
89
- r2 = requests.get(link)
90
-
91
-
92
-
93
- soupFinal = BeautifulSoup(r2.text, "html.parser")
77
+ soupFinal = BeautifulSoup(r.text, "html.parser")
94
78
 
95
79
  soupRealTradeCollum = soupFinal.find("div", "post_content clearfix").text
96
80