【问题标题】:Sorting Python Inside A Lists在列表中对 Python 进行排序
【发布时间】:2023-01-11 10:37:33
【问题描述】:
data = []

while True:
    print(url)
    response = requests.get(url, headers=headers)
    soup = BeautifulSoup(response.content, 'html.parser')
    links = soup.select_one('li.page-item.nb.active')
    
    for links in soup.find_all("h6", {"class": "text-primary title"}):
        sublink = links.find("a").get("href")
        new_link = "LINK" + sublink
        response2 = requests.get(new_link)
        soup2 = BeautifulSoup(response2.content, 'html.parser')
        
        # print('-------------------')
        heading = soup2.find('h1').text
        print(heading)

        table = soup2.find_all('tbody')[0]
        for i in table.find_all('td', class_='title'):
            movies = i.find('a', class_="text-primary")
            for movie in movies:
                data.append((heading,movie))
                
        df = pd.DataFrame(data=data)
        df.to_csv('list.csv', index=False, encoding='utf-8')

    next_page = soup.select_one('li.page-item.next>a')
    if next_page:
        next_url = next_page.get('href')
        url = urljoin(url, next_url)
    else:
        break

大家好!我怎样才能像这样在 CSV 上对结果进行排序,我尽力进行排序,但作为初学者,我很难做到这一点...................... .....

例子

Column1 | Column2  
James | Movie1, Movie2, Movie3
Peter | Movie1, Movie2, Movie3

想要我现在得到的是

Column1 | Column2
James, movie 1
James, movie 2
James, movie 3

【问题讨论】:

  • data变量在哪里定义的?
  • 以上为真我将更新代码
  • 欢迎来到堆栈溢出!好的起点是tourHow to Ask。请编辑您的代码,使其成为minimal reproducible example
  • 欢迎来到堆栈溢出。请阅读How to Askminimal reproducible example。如果问题是关于如何更改 DataFrame,那么不显示读取网页并解析数据的代码。只要确保它是清楚的之前 DataFrame 包含的确切内容期望的改变;并展示结果应该是什么,不仅仅是你现在得到的。另外:这里的部分代码应该进行排序吗?我没有看到任何试图做排序之类的事情。我们只能回答有关代码的问题这实际上向我们展示了.

标签: python


【解决方案1】:

不要遍历电影,而是使用", ".join(movies)

while True:
    print(url)
    response = requests.get(url, headers=headers)
    soup = BeautifulSoup(response.content, 'html.parser')
    links = soup.select_one('li.page-item.nb.active')
    
    for links in soup.find_all("h6", {"class": "text-primary title"}):
        sublink = links.find("a").get("href")
        new_link = "LINK" + sublink
        response2 = requests.get(new_link)
        soup2 = BeautifulSoup(response2.content, 'html.parser')
        
        # print('-------------------')
        heading = soup2.find('h1').text
        print(heading)

        table = soup2.find_all('tbody')[0]
        for i in table.find_all('td', class_='title'):
            movies = i.find('a', class_="text-primary")
            data.append((heading, ", ".join(movies)))
                
        df = pd.DataFrame(data=data)
        df.to_csv('list.csv', index=False, encoding='utf-8')

    next_page = soup.select_one('li.page-item.next>a')
    if next_page:
        next_url = next_page.get('href')
        url = urljoin(url, next_url)
    else:
        break

【讨论】:

    猜你喜欢
    • 2013-10-12
    • 1970-01-01
    • 2018-04-17
    • 1970-01-01
    • 2016-06-11
    • 2012-07-16
    • 2012-11-21
    相关资源
    最近更新 更多