复制字符串写入文件我真的不知道Python和我研究了很多,但是这是我能想出如何从网页在Python
import urllib2
import re
file = open('C:\Users\Sadiq\Desktop\IdList.txt', 'w')
for a in range(1,11):
s = str(a)
url='http://fanpagelist.com/category/top_users/view/list/sort/fans/page%s' + s
page = urllib2.urlopen(url).read()
for x in range(1,21):
id = re.search('php?id=(.+?)"',page)
file.write(id)
file.close()
我最好的代码试图复制身份证号码。在网页的像这样
HREF = “/ like_box.php?ID = 6679099553”
我只想写一个txt文件在新行数。有10个网页我想刮,我只想从每页的前20个ID。 但是,当我运行我的代码时,它显示403错误 如何做到这一点?
这是完全错误
C:\Users\Sadiq\Desktop>extractId.py
Traceback (most recent call last):
File "C:\Users\Sadiq\Desktop\extractId.py", line 7, in <module>
page = urllib2.urlopen(url).read()
File "C:\Python27\lib\urllib2.py", line 154, in urlopen
return opener.open(url, data, timeout)
File "C:\Python27\lib\urllib2.py", line 437, in open
response = meth(req, response)
File "C:\Python27\lib\urllib2.py", line 550, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Python27\lib\urllib2.py", line 475, in error
return self._call_chain(*args)
File "C:\Python27\lib\urllib2.py", line 409, in _call_chain
result = func(*args)
File "C:\Python27\lib\urllib2.py", line 558, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 403: Forbidden
打印网址,并看到,这是不正确。如果你使用'+',那么你不需要'%s'。要连接两个字符串,你需要'“A”+“B”或“A%s”%“B”' – furas
btw:'write()'不会添加'“\ n”'所以你需要'写(id +“\ n”)' – furas
谢谢,但仍然没有帮助。我仍然收到相同的错误 –