pycharm怎么导入本地包作为module
2 应该是你当前文件夹里面有一个叫urllib.py的文件,是你自己创建的.
最近在对照Bucky Roberts的tutorial学习python,遇到一些问题,就顺便一起问掉好了。
1. project interpreter的module导入,没有可以添加的module,下图:
2. 从网站上下载图片(这段代码别人运行OK的,但是我会有错误代码)
import random
import urllib
def download_web_image (url):
name = random.randrange(1,1000)
full_name = str(name) + ".jpg"
urllib.urlretrieve(url,full_name)
download_web_image("ntoronto.com/wp-content/uploads/2012/07/self-image.jpg")
错误提示:
E:\Python\python.exe C:/Users/Administrator/PycharmProjects/untitled/image.py
Traceback (most recent call last):
File "C:/Users/Administrator/PycharmProjects/untitled/image.py", line 9, in <module>
download_web_image("hoachintoronto.com/wp-content/uploads/2012/07/self-image.jpg")
File "C:/Users/Administrator/PycharmProjects/untitled/image.py", line 7, in download_web_image
urllib.urlretrieve(url,full_name)
AttributeError: 'module' object has no attribute 'urlretrieve'
Process finished with exit code 1
3. 从网站上下载文件: 这个因为不知道别人是否运行OK,所以不确定代码有没有问题:
from urllib import request
goog_url = 'http://real-chart.finance.yahoo.com/table.csv?s=GOOG&d=9&e=11&f=2015&g=d&a=2&b=27&c=2014&ignore=.csv'
def download_stock_data (csv_url):
response = request.urlopen(csv_url)
csv = response.read()
csv_str = str(csv)
lines = csv_str.split('\\n')
dest_url = r'goog.csv'
fx = open(dest_url,'w')
for line in lines:
fx.write(line + '\n')
fx.close()
download_stock_data(goog_url)
以上求解,谢谢~!! 等我可以导入module我就可以build a web crawler惹!
T T 谢谢。。。