Csv, tracing.



  • #! usr/bin python3
    
    import csv
    import urllib.request
    from bs4 import BeautifulSoup
    
    def get_html(url):
        response = urllib.request.urlopen(url)
        return response.read()
    
    
    f = open('text.txt', 'r')
    l = [line.strip() for line in f]
    
    def parse(html):
        soup = BeautifulSoup(html)
        search = soup.find('div', class_='search-total js-search-total')
        span = soup.find('span', class_='search-message js-page-title')
        spantext = span.text
        searchtext = search.text
    
        save (searchtext, spantext, 'project.csv')
    
    def save(searchtext, spantext, path):
        with open(path, 'w') as csvfile:
            writer = csv.writer(csvfile)
            writer.writerow(('Название', 'Кол-во'))
            for row in l:
                writer.writerow((spantext, searchtext))
            csvfile.close()
    
    
    def main():
        for url in l:
            parse(get_html(url))
    
    if __name__ == '__main__':
        main()
    

    In project.csv, two lines with the same data are eventually produced, although two urns in text.txt are different, and their data are also different.

    Where do you think you'?

    p.s. may anyone know how to verify the existence of a theater in html?



  • The code must look like:

    def parse(html):
        soup = BeautifulSoup(html)
        search = soup.find('div', class_='search-total js-search-total')
        span = soup.find('span', class_='search-message js-page-title')
        return span.text, search.text
    

    def save(search, span, writer):
    writer.writerow(span, search)

    def main():
    with open('text.txt', 'r') as f:
    with open('project.csv', 'w') as csvfile:
    writer = csf.writer(csvfile)
    writer.writerow('Name', 'Amount')
    for line in f:
    span, search = parse(get_html(line.strip()))
    save(search, span, writer)

    By the way, since you're like that. pythonic You use it. withclose(csvfile) It's completely unnecessary.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2