パート 1 からパート 2 にスムーズに移行し、結果をパート 3 に保存するにはどうすればよいですか? これまでのところ、パート 2 に自分で挿入しない限り、スクレイピングされた URL リンクを解析できませんでした。また、最後の URL リンクが他のすべての URL リンクを上書きしたため、出力結果を保存できませんでした。
import urllib
import mechanize
from bs4 import BeautifulSoup
import os, os.path
import urlparse
import re
import csv
パート1:
path = '/Users/.../Desktop/parsing/1.html'
f = open(path,"r")
if f.mode == 'r':
contents = f.read()
soup = BeautifulSoup(content
search = soup.findAll('div',attrs={'class':'mf_oH mf_nobr mf_pRel'})
searchtext = str(search)
soup1 = BeautifulSoup(searchtext)
for tag in soup1.findAll('a', href = True):
raw_url = tag['href'][:-7]
url = urlparse.urlparse(raw_url)
p = "http"+str(url.path)
パート2:
for i in url:
url = "A SCRAPED URL LINK FROM ABOVE"
homepage = urllib.urlopen(url)
soup = BeautifulSoup(homepage)
for tag in soup.findAll('a',attrs={'name':'g_my.main.right.gifts.link-send'}):
searchtext = str(tag['href'])
original = searchtext
removed = original.replace("gifts?send=", "")
print removed
パート3
i = 0
for i in removed:
f = open("1.csv", "a+")
f.write(removed)
i += 1
f.close
更新 1.アドバイスの後、私はまだこれを取得します: Traceback (most recent call last): File "page.py", line 31, in website = urllib.urlopen(url) File "/System/Library/Frameworks/Python. Framework/Versions/2.7/lib/python2.7/urllib.py"、87 行目、urlopen の return opener.open(url) ファイル "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2. 7/urllib.py"、180 行目、開いている fullurl = unwrap(toBytes(fullurl)) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py"、1 行目1057、unwrap url = url.strip() AttributeError: 'ParseResult' オブジェクトに属性 'strip' がありません