複製字符串寫入文件我真的不知道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
謝謝,但仍然沒有幫助。我仍然收到相同的錯誤 –