Core code:
def ipPools(numPage):
headers = randomHeads()
url = 'http://www.xicidaili.com/nn/'
saveFsvFile = open('ips.csv', 'wb')
writer = csv.writer(saveFsvFile)
for num in range(1, numPage + 1):
full_url = url + str(num)
re = requests.get(full_url, headers=headers)
soup = BeautifulSoup(re.text, 'lxml')
res = soup.find(id="ip_list").find_all('tr')
for item in res:
try:
temp = []
tds = item.find_all('td')
proxyIp = tds[1].text.encode("utf-8")
proxyPort = tds[2].text.encode("utf-8")
temp.append(proxyIp)
temp.append(proxyPort)
writer.writerow(temp)
print('保存为excel成功!')
except IndexError:
pass
Points to note.
Be sure to convert str to bytes :
str.encode("utf-8")
python36 file method to open
open('ips.csv', 'wb') change wb to w I got an error right here. If there is the same error can, as a reference it!
Similar Posts:
- Python scrapy/ ERROR: Spider must return request, item, or None, got ‘Tag ‘insettlement program
- Error reporting and resolution of Python 3 using binascii method
- AttributeError: ‘NoneType’ object has no attribute ‘split’ [How to Solve]
- Python Open File SyntaxError: (unicode error) ‘unicodeescape’ codec can’t decode bytes in …
- attributeerror: ‘str’ object has no attribute ‘decode’
- Transcoding of system.web.httputility.urlencode in C #
- Each child in an array or iterator should have a unique “key” prop. Check the render method of `Tabl
- [Solved] HttpClient HttpRequestHeaders.From Error: The specified value is not a valid ‘From’ header string.
- Syntax error: word unexpected (expect
- [Solved] OpenCV 4 (C++) Error: “error: ‘CV_FOURCC’ was not declared in this scope”