从多个列表中提取特定行索引错误

时间:2017-11-08 06:09:00

标签: python

var query = this.value;

var myPath = window.location.href.substring(window.location.href.indexOf("/myapp"), window.location.href.indexOf(".html"));
var myHost = window.location.href.substring(0, window.location.href.indexOf("/myapp"));
query = encodeURIComponent(query);

if(myPath!==null){
    query = query + "mysite:" + myHost + myPath;
}

我正在尝试打印例如。只有列表中的行直到94.5W,但它给出了一个超出范围误差的索引。 这是我得到的输出,如果我这样做:

import urllib.request
import bs4 as bs
import requests 
import re

url = " https://www.weather-forecast.com/locations/Kansas-City-
1/forecasts/latest"
request = requests.get(url) 
response = request.text 

soup = bs.BeautifulSoup(response,'lxml')

for para in soup.find_all('p'):

    a = para.text.split('/n')

    print(a[1])

但我只想打印我想要的特定行

1 个答案:

答案 0 :(得分:0)

您在循环中声明a变量并打印它,因此列表只有一个元素,并且每次都会被覆盖。将a声明和print()移到for循环之外应该可以解决问题

import bs4 as bs
import requests 
import re

url = " https://www.weather-forecast.com/locations/Kansas-City-1/forecasts/latest"
request = requests.get(url) 
response = request.text 

soup = bs.BeautifulSoup(response,'lxml')

a=[]
for para in soup.find_all('p'):

    a.append(para.text.split('/n'))

print(a[1])