0

I am trying to fetch recurring information (product names) from an e-commerce website. To do this, I use XPath. I have followed this tutorial to do so.

from lxml import html
import requests

page = requests.get("https://search.rakuten.co.jp/search/mall/-/565210/tg1000768/")
tree = html.fromstring(page.content)

urls = tree.xpath('//div[@class="image"]/a/img/@src')
titles = tree.xpath('//div[@class="content title"]/h2/a/text()')

print(len(titles))
print(titles)

The print(len(titles)) displays a correct number. However, the print(titles) raises an error

print(titles)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 2-15: ordinal not in range(128)

What am I supposed to do ?

pp492
  • 511
  • 1
  • 3
  • 12
  • 1
    This means your console (IDE, shell, whatever) does not support all the characters in the scraped data. Try writing to a file instead. – Chillie Sep 14 '18 at 09:45
  • 2
    Possible duplicate of [UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 20: ordinal not in range(128)](https://stackoverflow.com/questions/9942594/unicodeencodeerror-ascii-codec-cant-encode-character-u-xa0-in-position-20) – Andersson Sep 14 '18 at 09:45
  • 1
    @Chillie That was it, thank you! – pp492 Sep 14 '18 at 09:50

0 Answers0