【问题标题】:The index in the list giving one letter of every row instead of complete single row in python列表中的索引给出每一行的一个字母,而不是python中的完整单行
【发布时间】:2022-09-29 20:20:16
【问题描述】:

我只是从网页上抓取链接。我需要循环所有 URL,因为我希望在单个索引上有一个 URL,但是在我将 URLS 放入列表格式并尝试获取单个 URL 而不是完整 URL 之后,我得到列表中每个 URL 的单个字母。这是代码:

import pandas as pd
from bs4 import BeautifulSoup
import requests
from csv import writer

url = \'https://www.pwcs.edu/index\'
req = requests.get(url)

soup = BeautifulSoup(req.text, \'lxml\')
lists = soup.find(\'div\', class_=\'school-dropdown-col\')
for link in lists.find_all(\'a\', href=True):
    x = list([link[\'href\']])
    print(x[0][3])

    标签: python web-scraping


    【解决方案1】:

    只需简单地删除最后一行中的最后一个'[3]',它应该可以工作:

    import pandas as pd
    from bs4 import BeautifulSoup
    import requests
    from csv import writer
    
    url = 'https://www.pwcs.edu/index'
    req = requests.get(url)
    
    soup = BeautifulSoup(req.text, 'lxml')
    lists = soup.find('div', class_='school-dropdown-col')
    for link in lists.find_all('a', href=True):
        x = list([link['href']])
        print(x[0])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-04
      • 2015-04-17
      相关资源
      最近更新 更多