我有一个脚本,我试图让用户输入命令的PATH。大多数情况下,此命令将存在于用户目录中,因此通常会使用〜符号输入PATH。
代码看起来基本上是这样的(为了简化调试):
import urllib2
from bs4 import BeautifulSoup
import csv
CSVfile = open('srData.csv')
fileReader = csv.reader(CSVfile)
Data = list(fileReader)
i = 0
with open('blank.csv','wb') as f1:
writer=csv.writer(f1, delimiter='\t',lineterminator='\n',)
for id in Data:
try:
soup = BeautifulSoup(urllib2.urlopen("http://www.ncbi.nlm.nih.gov/pubmed/" & id).read())
jouryear = soup.find_all(attrs={"class": "cit"})
year = jouryear[0].get_text()
yearlength = len(year)
titleend = year.find(".")
year1 = titleend+2
year2 = year1+1
year3 = year2+1
year4 = year3+1
year5 = year4+1
published_date = (year[year1:year5])
title = soup.find_all(attrs={"class": "rprt abstract"})
title = (title[0].h1.string)
abstract = (soup.find_all(attrs={"class": "abstr"}))
abstract = (abstract[0].p.string)
writer.writerow([id, published_date, title, abstract])
except:
writer.writerow([id, "error"])
print (id)
i = i+1
print i
但我只是得到一个错误说〜/ my / path不是文件或目录。如果我将错误中的命令复制并粘贴到终端中,则可以正常工作。
有什么想法吗?