BeautifulSoupとRegexを使用してthis websiteをスクレイプしようとしています。そうしている間、私は "二重引用符"を持っていた質問に遭遇し、 "二重引用符"を置き換えて.txtファイルとして保存したいと考えました。しかし、それは "二重引用符"を置き換えるものではありません。 .replace()メソッドを試しましたが失敗しました。コードは次のとおりです。Python Beautiful Soup and Regex - 二重引用符が置き換えられない
url = 'http://www.sanfoundry.com/operating-system-mcqs-process-scheduling-queue/'
r = requests.get(url)
soup = bs(r.content)
data = soup.find_all('div', {'class':'entry-content'})
data1 = data[0].text
pattern = r'^\d{1,2}[\.|\)]([\s|\S].*)|(^[a-z]\)\s.*)|^View Answer\s?(Answer:.*)'
#pattern = r'^\d{1,2}[\.|\)]\s*(.*)|(^[a-z]\)\s.*)|^View Answer\s?(Answer:.*)'
reg = re.compile(pattern)
#with open(r'C:\Users\dhvani\Google Drive\Python\Data Scraping\byb.txt', 'a') as f:
with open(r'C:\Users\Jeri_Dabba\Google Drive\Python\Data Scraping\byb.txt', 'a') as f:
for i in data1.split('\n'):
if reg.search(i).group(1):
y = reg.search(i).group(1)
y = y.replace('"', '')
f.write(y + "\n")
私が.txtファイルをチェックしたとき、「二重引用符」は置き換えられませんでした。何が問題なのでしょうか?
私はPythonの初心者です。
既にあなたは正規表現を実行するプレーンテキストを持って、ここにそれを投稿してください。 –