Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9213637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:47:41+00:00 2026-06-18T01:47:41+00:00

I’m learning how to write scrapers using Python in Scraperwiki. So far so good,

  • 0

I’m learning how to write scrapers using Python in Scraperwiki. So far so good, but I have spent a couple of days scratching my head now over a problem I can’t get my head around. I am trying to take all links from a table. It works, but from the list of links which go from 001 to 486 it only ever starts grabbing them at 045. The url/source is just a list of cities on a website, the source can be seen here:
http://www.tripadvisor.co.uk/pages/by_city.html and the specific html starts here:

  </td></tr>
  <tr><td class=dt1><a href="by_city_001.html">'s-Gravenzande, South Holland Province&nbsp;&nbsp;&nbsp;-&nbsp;&nbsp;&nbsp;Aberystwyth, Ceredigion, Wales</a></td>
  <td class=dt1><a href="by_city_244.html">Los Corrales de Buelna,  Cantabria&nbsp;&nbsp;&nbsp;-&nbsp;&nbsp;&nbsp;Lousada, Porto District, Northern Portugal</a></td>
  </tr>
  <tr><td class=dt1><a href="by_city_002.html">Abetone, Province of Pistoia, Tuscany&nbsp;&nbsp;&nbsp;-&nbsp;&nbsp;&nbsp;Adamstown, Lancaster County, Pennsylvania</a>       /td>
  <td class=dt1><a href="by_city_245.html">Louth, Lincolnshire, England&nbsp;&nbsp;&nbsp;-&nbsp;&nbsp;&nbsp;Lucciana, Haute-Corse, Corsica</a></td>
  </tr>
  <tr><td class=dt1><a href="by_city_003.html">Adamswiller, Bas-Rhin, Alsace&nbsp;&nbsp;&nbsp;-&nbsp;&nbsp;&nbsp;Aghir, Djerba Island, Medenine Governorate</a>       </td>
  <td class=dt1><a href="by_city_246.html">Luccianna, Haute-Corse, Corsica&nbsp;&nbsp;&nbsp;-&nbsp;&nbsp;&nbsp;Lumellogno, Novara, Province of Novara, Piedmont</a></td>
  </tr>

What I am after is the links from “by_city_001.html” through to “by_city_486.html”. Here is my code:

  def scrapeCityList(pageUrl):

html = scraperwiki.scrape(pageUrl)
root = lxml.html.fromstring(html)

print html

links = root.cssselect('td.dt1 a')

for link in links:

    url = 'http://www.tripadvisor.co.uk' + link.attrib['href']
    print url 

Called in the code as follows:

  scrapeCityList('http://www.tripadvisor.co.uk/pages/by_city.html')

Now when I run it, it only ever returns the links starting at 0045!

The output (045~486)

http://www.tripadvisor.co.ukby_city_045.html
http://www.tripadvisor.co.ukby_city_288.html
http://www.tripadvisor.co.ukby_city_046.html
http://www.tripadvisor.co.ukby_city_289.html
http://www.tripadvisor.co.ukby_city_047.html
http://www.tripadvisor.co.ukby_city_290.html and so on...

I’ve tried changing the selector to:

  links = root.cssselect('td.dt1')

And it grabs 487 ‘elements’ like this:

<Element td at 0x13d75f0>
<Element td at 0x13d7650>
<Element td at 0x13d76b0>

But I’m not able to get the ‘href’ value from this. I can’t figure out why it loses the first 44 links when I select ‘a’ in the cssselect line. I’ve looked at the code but I have no clue.

Thanks in advance for any help!

Claire

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T01:47:42+00:00Added an answer on June 18, 2026 at 1:47 am

    Your code works fine. You can see it in action here: https://scraperwiki.com/scrapers/tripadvisor_cities/

    I’ve added in saving to the datastore so you can see that it actually processes all the links.

    import scraperwiki
    import lxml.html
    
        def scrapeCityList(pageUrl):
        html = scraperwiki.scrape(pageUrl)
        root = lxml.html.fromstring(html)
        links = root.cssselect('td.dt1 a')
        print len(links)
        batch = []
        for link in links[1:]: #skip the first link since it's only a link to tripadvisor and not a subpage
            record = {}
            url = 'http://www.tripadvisor.co.uk/' + link.attrib['href']
            record['url'] = url
            batch.append(record)
        scraperwiki.sqlite.save(["url"],data=batch)
    
    scrapeCityList('http://www.tripadvisor.co.uk/pages/by_city.html') 
    

    If you use the second css selector:

    links = root.cssselect('td.dt1')
    

    then you are selecting the td element and not the a element (which is a sub-element of the td). You could select the a by doing this:

    url = 'http://www.tripadvisor.co.uk/' + link[0].attrib['href']
    

    where you are selecting the first sub-element of the td (that’s the [0]).

    If you want to see all attributes of an element in lxml.html then use:

    print element.attrib
    

    which for the td gives:

    {'class': 'dt1'}
    {'class': 'dt1'}
    {'class': 'dt1'}
    ...
    

    and for the a:

    {'href': 'by_city_001.html'}
    {'href': 'by_city_244.html'}
    {'href': 'by_city_002.html'}
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
This could be a duplicate question, but I have no idea what search terms
I have thousands of HTML files to process using Groovy/Java and I need to
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I am using JSon response to parse title,date content and thumbnail images and place

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.