我有以下代码:
import re
from bs4 import BeautifulSoup
f = open('AIDNIndustrySearchAll.txt', 'r')
g = open('AIDNurl.txt', 'w')
t = f.read()
soup = BeautifulSoup(t)
list = []
counter = 0
for link in soup.find_all("a"):
a = link.get('href')
if re.search("V", a) != None:
list.append(a)
counter = counter + 1
new_list = ['http://www.aidn.org.au/{0}'.format(i) for i in list]
output = "\n".join(i for i in new_list)
g.write(output)
print output
print counter
f.close()
g.close()
它基本上是通过一个保存的HTML页面并拉动我感兴趣的链接。我是Python的新手,所以我确信代码很糟糕但它(几乎)正在工作;)
目前的问题是它返回的是每个链接的两个副本,而不是一个。我确信这与设置循环的方式有关,但有点卡住了。
我欢迎任何有关此问题的帮助(如果需要,我可以提供更多详细信息 - 例如HTML和我正在寻找的链接的更多信息)以及任何一般代码改进,以便我尽可能多地学习。
答案 0 :(得分:2)
由于您也要求代码优化,我会将我的建议作为答案发布。随意!
from bs4 import BeautifulSoup
f = open('AIDNIndustrySearchAll.txt', 'r')
t = f.read()
f.close()
soup = BeautifulSoup(t)
results = [] ## 'list' is a built-in type and shouldn't be used as variable name
for link in soup.find_all('a'):
a = link.get('href')
if 'V' not in a:
results.append(a)
formatted_results = ['http://www.aidn.org.au/{0}'.format(i) for i in results]
output = "\n".join(formatted_results)
g = open('AIDNurl.txt', 'w')
g.write(output)
g.close()
print output
print len(results)
这仍然无法解决您的原始问题,请参阅我和其他人的问题评论。
答案 1 :(得分:2)
正如其他人在评论中指出的那样,你的循环看起来很好,所以重复可能在HTML本身。如果您可以共享HTML文件的链接,我们可能会提供更多帮助。
至于一般的代码改进,以下是我如何处理这个问题:
from bs4 import BeautifulSoup
soup = BeautifulSoup(open('AIDNIndustrySearchAll.txt', 'r'))
# create a generator that returns actual href entries
links = (x.get('href') for x in soup.find_all('a'))
# filter the links to only those that contain "V" and store it as a
# set to remove duplicates
selected = set(a for a in links if "V" in a)
# build output string using selected links
output = "\n".join('http://www.aidn.org.au/{0}'.format(a) for a in selected)
# write the string to file
with open('AIDNurl.txt', 'w') as f:
f.write(output)
print output
print len(selected) # print number of selected links
答案 2 :(得分:0)
Find_all返回所有元素的列表。如果你只想要第一个,你可以这样做:for link in soup.find_all("a")[:1]:
。目前还不清楚为什么列表是链接的副本。您可以使用print语句更好地了解代码。打印列表和列表的长度等。或者您可以使用pdb