skip to Main Content

So I Wrote the code and ran it and got the .xlsx file but the output is not as the same order of the Url list i put in the code.

#importing the libraries
import re
import lxml
import  chardet
from os import truncate
import bs4
from bs4 import BeautifulSoup
import multiprocessing
import requests
import pandas as pd
from fake_useragent import UserAgent
import numpy as np

urls = list(('https://isabad.com/advanced-professional-email-templates-opencart-extension' ,
'https://isabad.com/seo-basic-pack-opencart-extension',
'https://isabad.com/x-shipping-pro',
'https://isabad.com/bot-blocker-opencart-extension',
'https://isabad.com/opencart-mobile-application'
))

dit = {}
user_agent = UserAgent()
for url in urls:
        data = requests.get(url, headers={"user-agent": user_agent.chrome})
        soup = bs4.BeautifulSoup(data.content, "lxml")
        dit[url] = soup.find_all("title")
        ex = pd.DataFrame({"title": dit ,})
        print(ex)
        ex.to_excel('sasa.xlsx', index=False, engine='xlsxwriter')


How Can I fix this problem?

2

Answers


  1. You are using the set data structure for storing the list of URLs and the set data structure in python is an unordered data structure. To have the output in the same order, you should store the URLs in list data structure as follows:

    urls = [
      'https://www.sample.com/search/category-mobile/' ,
      'https://www.sample.com/search/category-tablet-ebook-reader',
      'https://www.sample.com/search/category-laptop/',
      'https://www.sample.com/search/category-computer-parts/',
      'https://www.sample.com/search/category-office-machines/'
    ]
    

    Cheers!

    Login or Signup to reply.
  2. use a list so the results would be in the same order that you defined.

    urls = ['https://www.sample.com/search/category-mobile/' ,
    'https://www.sample.com/search/category-tablet-ebook-reader',
    'https://www.sample.com/search/category-laptop/',
    'https://www.sample.com/search/category-computer-parts/',
    'https://www.sample.com/search/category-office-machines/'
    ]
    

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search