Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.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 当我';我正在尝试用csv.writer写入数据 导入请求 将urllib导入为ulb 进口稀土 导入csv temp=r“C:\Users\ATP\Desktop”+'\\'+“数据” 打开(temp+'.csv',a',换行符='')作为f: csvwriter=csv.writer(f,方言='excel') csvwriter.writerow(['ID'、'total'、'share'、'rate'、'employee'、'social'、'stack'、'environment'])) 对于范围(1184)内的i: url=( 'http://stockdata.stock.hexun.com/zrbg/data/zrbList.aspx?date={}-12-31&count=20&pname=20&titType=null&page={}&callback=hxbase\u json11571728040603'。格式( 2018年,i)) 标题={ “用户代理”:“Mozilla/5.0(Windows NT 6.1;Win64;x64)AppleWebKit/537.36(KHTML,类似Gecko)Chrome/62.0.3202.62 Safari/537.36”} req=ulb.request.request(url=url,headers=headers) 响应=ulb.request.urlopen(请求) html1=response.read().decode('gbk') id=re.findall(r’(?_Python_Python 3.x_Web Crawler - Fatal编程技术网

Python 当我';我正在尝试用csv.writer写入数据 导入请求 将urllib导入为ulb 进口稀土 导入csv temp=r“C:\Users\ATP\Desktop”+'\\'+“数据” 打开(temp+'.csv',a',换行符='')作为f: csvwriter=csv.writer(f,方言='excel') csvwriter.writerow(['ID'、'total'、'share'、'rate'、'employee'、'social'、'stack'、'environment'])) 对于范围(1184)内的i: url=( 'http://stockdata.stock.hexun.com/zrbg/data/zrbList.aspx?date={}-12-31&count=20&pname=20&titType=null&page={}&callback=hxbase\u json11571728040603'。格式( 2018年,i)) 标题={ “用户代理”:“Mozilla/5.0(Windows NT 6.1;Win64;x64)AppleWebKit/537.36(KHTML,类似Gecko)Chrome/62.0.3202.62 Safari/537.36”} req=ulb.request.request(url=url,headers=headers) 响应=ulb.request.urlopen(请求) html1=response.read().decode('gbk') id=re.findall(r’(?

Python 当我';我正在尝试用csv.writer写入数据 导入请求 将urllib导入为ulb 进口稀土 导入csv temp=r“C:\Users\ATP\Desktop”+'\\'+“数据” 打开(temp+'.csv',a',换行符='')作为f: csvwriter=csv.writer(f,方言='excel') csvwriter.writerow(['ID'、'total'、'share'、'rate'、'employee'、'social'、'stack'、'environment'])) 对于范围(1184)内的i: url=( 'http://stockdata.stock.hexun.com/zrbg/data/zrbList.aspx?date={}-12-31&count=20&pname=20&titType=null&page={}&callback=hxbase\u json11571728040603'。格式( 2018年,i)) 标题={ “用户代理”:“Mozilla/5.0(Windows NT 6.1;Win64;x64)AppleWebKit/537.36(KHTML,类似Gecko)Chrome/62.0.3202.62 Safari/537.36”} req=ulb.request.request(url=url,headers=headers) 响应=ulb.request.urlopen(请求) html1=response.read().decode('gbk') id=re.findall(r’(?,python,python-3.x,web-crawler,Python,Python 3.x,Web Crawler,您正在重写for循环中的变量f。 将open语句更改为: 将open(temp+'.csv',a',newline='\n',encoding='gbk')作为fp:#作为f将覆盖foor循环的f 显然,将csvwriter=csv.writer(f,方言='excel')更改为csvwriter=csv.writer(fp,方言='excel') 记住这个错误,向自己解释为什么键入额外的字符和有意义的变量名非常有用。f还用作文件指针更改变量名,并查看for循环或在打开文件进行写入时更改这些变

您正在重写for循环中的变量f。 将open语句更改为:

将open(temp+'.csv',a',newline='\n',encoding='gbk')作为fp:#作为f将覆盖foor循环的f
显然,将
csvwriter=csv.writer(f,方言='excel')
更改为
csvwriter=csv.writer(fp,方言='excel')


记住这个错误,向自己解释为什么键入额外的字符和有意义的变量名非常有用。

f还用作文件指针更改变量名,并查看for循环或在打开文件进行写入时更改这些变量名后,csvwriter.writerow(像csvwriter.writerow(['aa','bb','cc','dd','ee','ff','gg','hh']),问题仍然存在。此外,每次在运行代码之前,我都会删除已经存在的csv文件。您确定吗?因为您是在附加模式下打开文件的,新条目可能就在附加模式的文件底部。但是我不明白与错误的关系。我尝试更改变量名称,并删除确实存在的csv文件。问题仍然没有解决。我还可以尝试其他方法吗?哦,天哪。问题是由重复变量名引起的。“f”在代码中的不同位置使用,并且混淆了。将“f”行中的“f”替换为“open xxxx as f”行中未使用的其他名称。数据将为b我用正确的方式写的。
import requests
import urllib as ulb
import re
import csv

temp= r"C:\Users\ATP\Desktop"+'\\'+"data"
with open(temp + '.csv', 'a', newline='') as f:
    csvwriter = csv.writer(f, dialect='excel')
    csvwriter.writerow(['ID', 'total', 'share', 'rate', 'employee', 'social', 'stake', 'environment'])
for i in range(1,184):
    url = (
        'http://stockdata.stock.hexun.com/zrbg/data/zrbList.aspx?date={}-12-31&count=20&pname=20&titType=null&page={}&callback=hxbase_json11571728040603'.format(
            2018, i))
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.62 Safari/537.36'}
    req = ulb.request.Request(url=url, headers=headers)
    response = ulb.request.urlopen(req)
    html1 = response.read().decode('gbk')

    id = re.findall(r'(?<=\()\d\d\d\d\d\d(?=\))',html1)
    total = re.findall(r'(?<=industryrate:\').+?(?=\')',html1)
    share = re.findall(r'(?<=stockNumber:\').+?(?=\')',html1)
    rate = re.findall(r'(?<=Pricelimit:\').+?(?=\')',html1)      
    employee = re.findall(r'(?<=lootingchips:\').+?(?=\')',html1)

    social = re.findall(r'(?<=Strongstock:\').+?(?=\')',html1)

    stake = re.findall(r'(?<=Scramble:\').+?(?=\')',html1)
    environment = re.findall(r'(?<=rscramble:\').+?(?=\')',html1)  

    for a,b,c,d,e,f,g,h in zip(id,total,share,rate,employee,social,stake,environment):

        with open(temp+'.csv','a',newline = '\n',encoding='gbk') as f:
            csvwriter = csv.writer(f,dialect='excel')
            csvwriter.writerow([a,b,c,d,e,f,g,h])