'NoneType'对象没有属性'find_all'

时间:2019-11-19 09:43:59

标签: python python-3.x

import sys,csv
import pandas as pd
from bs4 import BeautifulSoup
import requests
from lxml import html


url = r'https://agmarknet.gov.in/SearchCmmMkt.aspx?Tx_Commodity=137&Tx_State=0&Tx_District=0&Tx_Market=0&DateFrom=01-jan-2016&DateTo=19-nov-2019&Fr_Date=01-jan-2016&To_Date=19-nov-2019&Tx_Trend=2&Tx_CommodityHead=Ajwan&Tx_StateHead=--Select--&Tx_DistrictHead=--Select--&Tx_MarketHead=--Select--'

data = []

Res = requests.get(url)
Soup = BeautifulSoup(Res.content,'lxml')
#print(Soup.prettify())

##get_details = Soup.find_all('option')
##for element in get_details:
##    print(element['value'])

mylists = ['137','281','325','166','86','130','355','102','209','379','17','326','140','49','260','83','232','269','28','274','204','19','90','29','321','94','262','157','263','6','357','143','41','85','113','51','81','8','264','38','380','189','82','290','35','293','320','214','284','224','272','154','215','354','205','164','40','375','153','238','133','36','270','123','34','188','169','241','295','328','71','26','88','167','402','231','316','105','80','315','368','104','138','37','266','112','45','318','129','43','108','15','99','212','92','89','159','42','352','382','91','273','410','69','168','132','345','278','370','163','367','361','296','64','221','206','366','365','121','298','350','25','249','276','27','103','364','363','219','353','359','294','22','165','87','346','9','265','50','267','268','10','314','312','75','413','185','74','279','252','216','369','125','236','124','119','299','344','256','182','406','151','175','184','376','229','250','5','16','210','362','317','230','233','115','187','305','61','372','336','177','117','243','114','155','96','171','310','63','378','180','67','280','351','302','304','403','339','337','107','411','335','371','288','4','20','172','225','181','407','235','405','60','340','259','93','47','46','237','360','258','95','77','12','324','142','245','222','126','223','98','106','23','358','18','381','97','213','414','2','72','313','404','331','330','308','174','347','301','109','110','327','220','21','329','303','240','190','24','84','254','128','161','30','409','248','282','307','65','7','322','62','3','160','374','228','373','306','111','291','59','338','271','122','247','277','253','176','201','11','217','283','218','183','226','156','135','207','286','246','13','342','311','332','48','150','14','285','139','242','300','255','178','152','173','120','261','208','100','76','44','200','116','170','162','349','141','78','323','66','234','401','408','377','39','309','341','343','73','1','287','412','158','203','348','244','297']
for mylist in mylists:
    url = 'https://agmarknet.gov.in/SearchCmmMkt.aspx?Tx_Commodity='+mylist+'+&Tx_State=0&Tx_District=0&Tx_Market=0&DateFrom=01-jan-2016&DateTo=19-nov-2019&Fr_Date=01-jan-2016&To_Date=19-nov-2019&Tx_Trend=2&Tx_CommodityHead=Ajwan&Tx_StateHead=--Select--&Tx_DistrictHead=--Select--&Tx_MarketHead=--Select--'+ mylist
    soup = BeautifulSoup(Res.content,'lxml')
    table = soup.find('div',{'class':'tableagmark_new'})
    rows = table.find_all('tr')
    for row in rows:
        data.append(str(row.find_all('td').text))

这是我的代码,在迭代for row in rows

中发现错误
  

nonetype对象无法迭代

1 个答案:

答案 0 :(得分:1)

将for循环修改为:

for mylist in mylists:
    url = 'https://agmarknet.gov.in/SearchCmmMkt.aspx?Tx_Commodity='+mylist+'+&Tx_State=0&Tx_District=0&Tx_Market=0&DateFrom=01-jan-2016&DateTo=19-nov-2019&Fr_Date=01-jan-2016&To_Date=19-nov-2019&Tx_Trend=2&Tx_CommodityHead=Ajwan&Tx_StateHead=--Select--&Tx_DistrictHead=--Select--&Tx_MarketHead=--Select--'+ mylist
    soup = BeautifulSoup(Res.content,'lxml')
    table = soup.find('table',{'class':'tableagmark_new'})
    rows = table.find_all('tr')
    for row in rows:
        cols = row.find_all('td')
        for col in cols:
            data.append(col.txt)