2017-08-16 13 views
-1

私はいくつかの異なるものにpythonを使用しましたが、私は以前にウェブページで作業していませんでしたので、私は何をすべきか正確には分かりません。基本的に私はキーワードを見つけてそのキーワードを含む行を辞書に書きたいWebページを持っています。私は本当にこれについてどうやって行くのか分かりません。これは私がこれまで持っていたものです。どんな種類の指導にも感謝します。ウェブページから辞書への行

import numpy as np 
import sys, re 
import csv 
import pylab as pl 
import math 
import argparse 
from bs4 import BeautifulSoup 
import urllib 
from bs4 import BeautifulSoup 
import urllib.request 
import re 
import requests 
from urllib.request import urlopen 
import urllib 

link = 'https://docs.google.com/forms/d/e/1FAIpQLSf-c78D25Vd52Na0yx1bWjniINPuGmhx2kM3Nr3VNsMYmIpRQ/viewform?usp=sf_link' 

r = requests.get(link) 
data = r.text 
soup = BeautifulSoup(data, 'html.parser') 

quiz = {} 

tmp = soup.findAll('Short Answer', 'Fill in the Blank', 'Multiple Choice', 'Matching', 'Numeric Response') 

for i in tmp: 
    print(i.get_text()) 
+0

有用なタグをhtmlで検索し、bs4で選択して辞書に追加します。 –

+0

あなたが読んだのはhtmlファイルではないので、より良い結果を得るためには、beautifulsoupを使用してください。 –

+0

soup.findallを使ってみましたが、これで何ができるのか分かりません。私はそれが私が持っているキーワードのどれかを見つけて、辞書に全文を追加できるようにしたい。スタイルdict [キーワード] =文で。 – K22

答えて

0

私は文章がhtmljavascriptに埋め込まれていると思うので、その後、一度に各キーワード1を見つけるために、ループの中でPythonの正規表現モジュール、reを使用し、最初のjavascriptのテキストを取得:

quiz = {} 

terms = ['Short Answer', 'Fill in the Blank', 'Multiple Choice', 'Matching', 'Numeric Response'] 

# get the javascript content 
text = soup.find_all('script', type="text/javascript")[1].string 

# loop through the terms searching the javascript text and get the entire sentence 
for term in terms: 
    # Use RegEx to get the complete sentence between double quotes 
    line = re.findall(r"([\"]*?" + term + "[^\"]*\")", text) 
    quiz[term] = line # assign the sentence to the quiz entry 

その後、フォームdict[keyword] = sentenceで各文にアクセスすることができます。

print(quiz['Multiple Choice']) # [u'"Multiple Choice: In which of the following no information hiding is done ?"'] 

空ENの点に注意してください。試してみます:

print(quiz['Numeric Response']) # [] 

希望します。

関連する問題