Python webcrawler语法错误

Python webcrawler语法错误,python,urllib2,Python,Urllib2,我是一个非常新的python程序员, 此脚本将运行,但不会打印任何内容。 看起来数据是空的,或者没有打印。。。 谢谢你 import urllib2 i=1 while i<=1000: url = "http://www.uspto.gov/web/patents/classification/cpc/html/us" + str(i) + "tocpc.html" print url + '\n' data = urllib2.urlopen(url).read

我是一个非常新的python程序员, 此脚本将运行,但不会打印任何内容。
看起来数据是空的,或者没有打印。。。 谢谢你

import urllib2
i=1
while i<=1000:
    url = "http://www.uspto.gov/web/patents/classification/cpc/html/us" + str(i) + "tocpc.html"
    print url + '\n'
    data = urllib2.urlopen(url).read()
    print data
    #get the table data from dump
    #append to csv file
    i += 1
导入urllib2
i=1

而我首先:您正在导入urrlib2,但使用urllib

将第一行更改为

import urllib
让剧本适合我


也可以考虑在In(11000)中使用I,而不是while循环。

在访问<代码>时,未找到错误。http://www.uspto.gov/web/patents/classification/cpc/html/us1tocpc.html
。。您想要生成什么URL?并非所有1-1000的int都存在。然而,其中大约有500人这样做。因此,如果URL不存在,我希望它可以忽略。例如:125确实存在。谢谢!我改成了urllib,它也对我有用。我不知道为什么urllib2不工作,但是谢谢!urllib2基本上也可以工作,但如果返回404,则返回异常,因此脚本以i=1退出。