j简单的遍历问题
import requestsfrom lxml import etree
import time
for i in range(1,1001):
url='http://glidedsky.com/level/web/crawler-basic-2?page=%d'%i
print(url)
headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36 Edg/88.0.705.56','Cookie': '_ga=GA1.2.1964518107.1612749372; __gads=ID=f81869fd6d870a7d-2245789af6c50099:T=1612749370:RT=1612749370:S=ALNI_MabXsiCH5EH1DFG9kH9Gdc6zxlUWA; footprints=eyJpdiI6IkllcFwvWnhqTjZ5alRtSDJwdGMzbnBBPT0iLCJ2YWx1ZSI6InF3TGY2OWM5akhjbkhGbGFzN01mY2t1XC9idFZUOVRTNlhraHh6M3VZZUVTaXBmdTBSOVpDbGYwTU55ZG14Vk1RIiwibWFjIjoiNTM1NjE4MTllNTBkOTkwYWQwM2YwOWExOTY5NWIwN2JkZTU3NjAzMGE3MzkyM2UwYjJhZjcxZTFjM2FiODNjMiJ9; _gid=GA1.2.1436864400.1614076469; remember_web_59ba36addc2b2f9401580f014c7f58ea4e30989d=eyJpdiI6InlKS3BseEV0b0RBdVRTS1lIRG40MXc9PSIsInZhbHVlIjoiQ3o5dmRRY2J0aitCdGtTdVJBcnAzbFwveEJJTTlsdnlkTmxSS2JMUW9aeVhHeDRtXC9ha0Y2cnd3YlJRSytYWmQ5bk1ocDY5cUFFcFlQOHdpc1RmYlE5bTFvVm1cL0JWaFZweVJhMURWS0ZFXC9DZDErUGJ6d3cyc0MyYmd6eEdJVmNSR0c0UEl1azBBczVLWjVUeU9pbWJLMldjMjI5QU00SmFDT2YzOFVGN1ljST0iLCJtYWMiOiJhN2E0YzgzMmE1N2ZhMDQ4MTE3ZDQyNjNmZTllZTUyYzllNjIyMDM3ZGE0YzBmM2Y0ZTllZjQwYzM5YjNiODA4In0%3D; Hm_lvt_020fbaad6104bcddd1db12d6b78812f6=1612788781,1614076479,1614084065,1614084146; XSRF-TOKEN=eyJpdiI6InhWOGFGVUpzS0Vjb3M3NFk4T1ZyT1E9PSIsInZhbHVlIjoidmdFZ0p5d1FQTXJjNklqZDVpc1BqNERDTDluQlFzam9sb0dOQkJ6ZVVlaFZQcXl5WnNubjJPKzBDU1lmTjVXTCIsIm1hYyI6ImFkOGY3N2IxNzE1Yjg0MGQ4NDk0NDI1OTY2N2U2MjJkYTk4MTA2YjM3YWUyMjlhOTEzNzcyN2I1YmYwNTI4MTcifQ%3D%3D; glidedsky_session=eyJpdiI6ImcwaHRRZTBLTE5YaU9VMk8za0M1WHc9PSIsInZhbHVlIjoidk1BUVFieXFvQkZCd2FkMENwN0dNQWZzNmdIRVc5aVBwUFFRb3hhdk5yWHVRTnJ2UUdkbFNwTjFvdDNsWkZjbSIsIm1hYyI6IjVhOWU2N2UyOGRiOTEzMDdmNTkwMTJjOTFlMGVlYWIwMWYyNzNlY2Q5ZGU1ZTBjNGQ4ZWFjNmRlNjIzOTEwZmIifQ%3D%3D; Hm_lpvt_020fbaad6104bcddd1db12d6b78812f6=1614084341'}
page=requests.get(url=url,headers=headers)
page.encoding='utf-8'
page_text=page.text
response=etree.HTML(page_text);all=0;all_digital=0
for digital in response.xpath('//div[@class="col-md-1"]/text()'):
all+=int(digital)
print(all)
all_digital+=all
print(all_digital)
print(all_digital)
我想第2个遍历完成后将all的值用增强字符给all_digital,可是整个程序下来all_digital没有累加,这是为什么呀,葵丘大佬们康康!! 没毛病啊,值是正常的,没看懂你所表达的意思。
for i in range(1,5):的时候,最后all_digital 为3200
all+=int(digital) 这是整形相加 z5560636 发表于 2021-2-24 09:21
没毛病啊,值是正常的,没看懂你所表达的意思。
for i in range(1,5):的时候,最后all_digital 为3200
all_digital+=all
我想在第一个遍历第一次遍历下第2个遍历每次完成遍历后将all的值用增强字符给all_digital,可是整个程序下来all_digital没有累加,all_digital始终是和all相等的 response=etree.HTML(page_text);all=0;all_digital=0
你把all_digital=0 写到循环外面不就可以了?
页:
[1]