Page MenuHomePhabricator

pywikibot sitelinks have changed?
Closed, ResolvedPublic

Description

In past versions of pywikibot I was able to do this:

import pywikibot

enwp = pywikibot.Site('en', 'wikipedia')
repo = enwp.data_repository()

enwp_page = pywikibot.Page(enwp, 'South Pole Telescope')
content = enwp_page.get()
wd_item = pywikibot.ItemPage.fromPage(enwp_page)
item_dict = wd_item.get()
print(item_dict['sitelinks']['commonswiki'].replace('South','Test'))

which would return

Category:Test Pole Telescope

With the current version of pywikibot, however, I get this:

Traceback (most recent call last):
  File "test.py", line 10, in <module>
    print(item_dict['sitelinks']['commonswiki'].replace('South','Test'))
AttributeError: 'SiteLink' object has no attribute 'replace'

Something has clearly changed, is this documented somewhere, and is there a new method to get the string of the sitelinks, please?

Event Timeline

Restricted Application added subscribers: pywikibot-bugs-list, Aklapper. · View Herald TranscriptOct 17 2020, 5:16 PM
Xqt added a subscriber: Xqt.EditedOct 17 2020, 5:46 PM

item_dict['sitelinks']['commonswiki'] is a SiteLink object which has a title property. Use print(item_dict['sitelinks']['commonswiki'].title.replace('South','Test')). No idea yet whether and when the result has been changed.

@Xqt Thanks! I was trying title() (as per wd_item.title() ), but I didn't try .title. Is the introduction of SiteLink objects documented somewhere please?

Xqt added a comment.EditedOct 18 2020, 7:18 AM

See also T128202. It was introduced with release 3.0.20190722. The new result is not a str anymore but a SiteLink which provides badges

Change 634768 had a related patch set uploaded (by Xqt; owner: Xqt):
[pywikibot/core@master] [doc] Additional hint to sitelink badges

https://gerrit.wikimedia.org/r/634768

Change 634768 merged by jenkins-bot:
[pywikibot/core@master] [doc] Additional hint to sitelink badges

https://gerrit.wikimedia.org/r/634768

Xqt closed this task as Resolved.Oct 18 2020, 10:35 AM
Xqt claimed this task.

Just a follow-up note in case others encounter this problem: in the end I had to use .ns_title() instead of .title, as .title does not include the namespace (e.g., 'Category:Blah' becomes 'Blah')!

Xqt added a comment.EditedMon, Jan 11, 8:12 AM

Just a follow-up note in case others encounter this problem: in the end I had to use .ns_title() instead of .title, as .title does not include the namespace (e.g., 'Category:Blah' becomes 'Blah')!

Yes, this is intentional. You have to use ns_title() or better canonical_title() to include the namespace. astext() which is the same as str includes the wikitext link bracket. Note: there are no equivalent title parameters like in Page.title(). But this could be another way to proceed: Convert the title to a Page object and use their title() method