Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Int值在for循环中重置_Python_Python 2.7_Web Scraping - Fatal编程技术网

Python Int值在for循环中重置

Python Int值在for循环中重置,python,python-2.7,web-scraping,Python,Python 2.7,Web Scraping,我用python2.7编写了一个web抓取脚本。总共有4486页。我必须从他们每个人身上提取标题。我用的是beautifulSoup。我运行了一个for循环来增加页码,因为url具有页面增量 每页有50个条目。但是,当我的总条目数为12850时,我的python程序停止运行,没有任何错误 import sys reload(sys) sys.setdefaultencoding('utf-8') from bs4 import BeautifulSoup import urllib stri

我用python2.7编写了一个web抓取脚本。总共有4486页。我必须从他们每个人身上提取标题。我用的是beautifulSoup。我运行了一个for循环来增加页码,因为url具有页面增量

每页有50个条目。但是,当我的总条目数为12850时,我的python程序停止运行,没有任何错误

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

from bs4 import BeautifulSoup
import urllib

string = 'https://marketpublishers.com/members/dummyurl/info.html?page='
for i in range(0,4487):
    myUrl = string+str(i)

    r = urllib.urlopen(myUrl).read()
    soup = BeautifulSoup(r, 'html.parser')

    raw = soup.find_all("div", class_='listItemDef')
    file = open('testfile.txt','a')
    for element in raw:
        file.write(element.a.get_text())
        file.write('\n')
    file.close()
我假设它与整数值不能超过256有关。
我如何克服这个问题?

首先是。。。为什么每次迭代都要重新打开并关闭文件?保持打开,然后关闭或使用上下文管理器。感谢您指出这一点。我会纠正的。Python中的整数什么时候可以不超过256?@aryamcarthy。事实上你是对的,但是我不知道是什么导致了这一点。使用调试器或一些日志打印语句来逐步遍历循环并查找错误,因为你已经给出了一个伪URL。